简体   繁体   English

博客 web 中的身份验证问题,后端使用节点、快递和 mongoose 和前端使用 ejs

[英]authentication problem in blogging web, backend with node, express and mongoose and front end with ejs

I am performing authentication and authorization using JWT and building rest apis to connect ejs and backend .我正在使用JWT执行身份验证和授权,并构建 rest apis 以连接ejsbackend After getting a person authenticated i am rendering to the blogger page of that user but when i clink on add block it says no token is passed but when i am doing it using postman then there is no issue it is getting token then.在让一个人通过身份验证后,我正在渲染到该用户的博客页面,但是当我点击添加块时,它说没有传递任何令牌,但是当我使用postman进行操作时,那么它就没有问题它正在获取令牌。 this is my code of rendering a blogger page after authentication:这是我在身份验证后呈现博客页面的代码:

router.post('/', async (req, res) => {
    const { error } = validate(req.body);
    if (error) return res.status(400).send(error.details[0].message);
    
    let user = await User.findOne({email:req.body.email});
    if (user) return res.status(400).send("user already registered");
    user = new User(_.pick(req.body,['name','email','password']));
    const salt = await bcrypt.genSalt(10);
    user.password = await bcrypt.hash(user.password,salt);
    await user.save();

    // const token= user.generateAuthToken();
    // res.header('x-auth-tocken',token).send({name:user.name, user:user._id,token:token});
    const token = jwt.sign({_id:this._id},config.get('jwtPrivateKey'));
    let blogs = await blogss.find();
    res.header('x-auth-token',token).render('bhome',{blogs:blogs,user:user});
 })

and this is my auth middleware:这是我的身份验证中间件:

module.exports = function (req ,res, next) {
    const token = req.header('x-auth-token');
    console.log(req.header('x-auth-token'));
    console.log('me here in auth');
    if(!token) return res.status(401).send('access denied because there is no token');
    try {
        const decoded = jwt.verify(token,config.get('jwtPrivateKey'));
        req.user = decoded;
        next();
    } catch (ex) {
        res.status(400).send('invalid taken');
    }
}

and this is the route after authentication which says token is not availible:这是身份验证后的路线,表示令牌不可用:

router.get('/addblog', auth, (req, res)=>{
    res.render('addblog');
});

The way you are handling the header is wrong and based on the limited code you provided, it seems to be the reason for possible error.您处理 header 的方式是错误的,根据您提供的有限代码,这似乎是可能出现错误的原因。 Instead of this doing the hard way, try using this gist and implement it as a util function in your frontend不要这样做,而是尝试使用这个要点并将其实现为前端的实用程序 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在单个 Azure Web 应用程序中具有 Angular 前端的 Node/Express 后端 - Node/Express Backend with Angular Front End in a Single Azure Web App 如何将数据从Express后端发送到EJS前端? - How to send data from express backend to ejs front end? express ejs前端访问API处理节点 - Accessing API handling node in express ejs front end 为 node js 后端 API 创建 Express 前端 - Creating Express front end for node js backend API 将数据从 Vue 前端传递到 Node/Express 后端 - Pass data from Vue front end to Node/Express backend 将图像文件从React前端上传到Node / Express / Mongoose / MongoDB后端(不起作用) - Upload image file from React front end to Node/Express/Mongoose/MongoDB back end (not working) PUG NodeJS Express 前端/后端无连接 - PUG NodeJS Express Front End / Backend no connection 连接 react 前端和 express 后端 - Connecting react front end and express backend 如何使用后端 Node.js Express-Sessions 对 React 前端进行身份验证? - How to authentication React Front End with back-end Node.js Express-Sessions? 我是否需要在使用 Node.js 创建后端服务和使用 ejs 创建带有视图引擎的前端部分的项目中使用 Babel? - Do I need to use Babel in project which created backend service with Node.js and front end part with view engine with ejs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM