Express doesn't set a cookie

后端 未结 10 1576
花落未央
花落未央 2020-12-14 01:45

I have problem with setting a cookies via express. I\'m using Este.js dev stack and I try to set a cookie in API auth /login route. Here is the cod

10条回答
  •  抹茶落季
    2020-12-14 02:15

    app.post('/api/user/login',(req,res)=>{
    
        User.findOne({'email':req.body.email},(err,user)=>{
            if(!user) res.json({message: 'Auth failed, user not found'})
            
            user.comparePassword(req.body.password,(err,isMatch)=>{
                if(err) throw err;
                if(!isMatch) return res.status(400).json({
                    message:'Wrong password'
                });
                user.generateToken((err,user)=>{
                    if(err) return res.status(400).send(err);
                    res.cookie('auth',user.token).send('ok')
                })
            }) 
        })
    });

    response

    res.cookie('auth',user.token).send('ok')

    server gives response ok but the cookie is not stored in the browser

    Solution :

    Add Postman Interceptor Extension to chrome which allows postman to store cookie in browser and get back useing requests.

提交回复
热议问题