koajs / jwt

Koa middleware for validating JSON Web Tokens
MIT License
1.34k stars 120 forks source link

Question - not an issue #173

Closed mylastore closed 4 years ago

mylastore commented 4 years ago

Does anyone knows how to store JWT token in a cookie with koa-jwt?

sdd commented 4 years ago

Setting a cookie is not something that koa-jwt has in its remit. Its quite simple to do without any libraries though:

    // somewhere inside a koa middleware function:

    const jwt = 'blah:blah:blah';   // if this is the JWT that you want to set as a cookie

    const cookieOptions = {
        // see here for options:
        // https://koajs.com/#ctx-cookies-set-name-value-options-
    };

    // COOKIE_NAME should match `opts.cookie` that is passed to koa-jwt,
    // if koa-jwt is authenticating against the cookie that you want to set
    ctx.cookies.set(COOKIE_NAME, jwt, options);