How to store jwt in cookie and pass it to authentication function when redirecting a page?

Ann*_*nie 6 node.js express jwt handlebars.js postman

I have a node.js express backend built with Postman and tested with Jest. I wrote a front end with hbs and the next step is to stitch them. However I still keep getting "please authenticate" error message that's from my auth function, which I guess is because I'm not successfully passing my jwt token.

So on login page (users/login) I want to login with email and password then I want to redirect to me page(users/me) where I can perform other stuff that belongs to this user.

Front-end login page code:

<section class="login-bg">
        <div class="login-form">
            <p>Welcome to Task Manager, please log in!</p>
            <form class="input-group" action="/users/login" method="POST">
                <label>Email:</label>
                <input type="email" name="email" placeholder="type your email" value="{?{user.email}}" required >
                <label>Password:</label>
                <input type="password" name="password" placeholder="type your password" value="{?{user.password}}" required>

                <button class="button" type="submit">Log In</button>
            </form>
        </div>
    </section>
Run Code Online (Sandbox Code Playgroud)

Back-end

in middleware/auth.js

const jwt = require('jsonwebtoken')
const User = require('../models/user')

const auth = async (req, res, next) => {
    try {
        const token = req.header('Authorization').replace('Bearer ', '')
        const decoded = jwt.verify(token, process.env.JWT_SECRET)
        const user = await User.findOne({_id: decoded._id, 'tokens.token': token})

        if (!user) {
            throw new Error()
        }

        req.token = token
        req.user = user
        next()

    } catch (error) {
        res.status(401).send({error: 'Please authenticate.'})
    }
}

module.exports = auth
Run Code Online (Sandbox Code Playgroud)

in src/routers/users.js

router.post('/login', async (req, res) => {
    try {
        const user = await User.findByCredentials(req.body.email, req.body.password)
        const token = await user.generateAuthToken()
        res.cookie('jwt',token, { httpOnly: true, secure: true, maxAge: 3600000 })
        res.redirect('/users/me')
    } catch (error) {
        res.status(400).send()
    }
})
Run Code Online (Sandbox Code Playgroud)

However, when I do console.log(document.cookie) in users/me it says undefined.

Then I have the cookie-parser installed and import to app.js, and try to write this part in src/routers/users.js:

router.get('/me', auth, async (req, res) => {
    console.log('Cookies: ', req.cookies)
    try {
        res.render('me', {name: user.name})
    } catch (error) {
        res.status(500).send()
    }
})
Run Code Online (Sandbox Code Playgroud)

but this console doesn't print anything, probably cos I am getting error from auth.

I also have a a js file attached to me page but I have no clue if I could write this way, probably wrong:

const userToken = document.cookie.jwt.token

fetch('/users/me', {
    method: 'POST',
    headers: {
     'Authorization': 'Bearer ' + userToken
    }
})
.then(res => res.json())
.then(data => { console.log(data) })
.catch(err => { console.log(err) })
Run Code Online (Sandbox Code Playgroud)

then in the Network / Headers, I have

Request URL:

http://localhost:3000/users/login

Request Method:

POST

Status Code:

302 Found

Remote Address:

Referrer Policy:

no-referrer-when-downgrade

Response Headers

Connection:

keep-alive

Content-Length:

62

Content-Type:

text/html; charset=utf-8

Date:

Fri, 07 Jun 2019 18:41:47 GMT

Location:

/users/me

Set-Cookie:

jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1Y2Y2NjNlMTQwMTQyYjE0MzhmZTJjNDMiLCJpYXQiOjE1NTk5MzI5MDd9.T_P8O-j98cs9gtahTzspJjx1qNMSe3M5OAySyeH25fs; Max-Age=3600; Path=/; Expires=Fri, 07 Jun 2019 19:41:47 GMT; HttpOnly; Secure

Vary:

Accept

X-Powered-By:

Express

There is no request cookies, only response cookies. I am not sure what those means...@_@

I want to pass the jwt to successfully login and render the me page properly, how could I do that?

sha*_*ncs 5

您的 jwt 令牌 cookie 不起作用,因为它secure: true在以下代码中声明了标志:

res.cookie('jwt',token, { httpOnly: true, secure: true, maxAge: 3600000 })
Run Code Online (Sandbox Code Playgroud)

这导致SecureHTTP 响应中的标志,表明此 cookie 仅在 HTTPS 环境下可用:

Set-Cookie:
jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1Y2Y2NjNlMTQwMTQyYjE0MzhmZTJjNDMiLCJpYXQiOjE1NTk5MzI5MDd9.T_P8O-j98cs9gtahTzspJjx1qNMSe3M5OAySyeH25fs; 
Max-Age=3600; Path=/; 
Expires=Fri, 07 Jun 2019 19:41:47 GMT; HttpOnly; Secure
Run Code Online (Sandbox Code Playgroud)

由于您的请求 URL 使用 HTTP ( http://localhost:3000/users/login),浏览器将忽略 cookie。