diff --git a/lib/index.js b/lib/index.js index 354f2c3..8a90c01 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,7 +6,6 @@ const unless = require('koa-unless'); module.exports = opts => { opts = opts || {}; opts.key = opts.key || 'user'; - opts.tokenKey = opts.tokenKey || 'token'; const tokenResolvers = [resolveCookies, resolveAuthorizationHeader]; diff --git a/test/test.js b/test/test.js index b11bc2a..93c0cef 100644 --- a/test/test.js +++ b/test/test.js @@ -385,6 +385,31 @@ describe('success tests', () => { .expect(validUserResponse) .end(done); }); + + it('should not overwrite ctx.state.token on successful token verification if opts.tokenKey is undefined', done => { + const validUserResponse = res => res.body.token === "DONT_CLOBBER_ME" && "ctx.state.token not clobbered"; + + const secret = 'shhhhhh'; + const token = jwt.sign({foo: 'bar'}, secret); + + const app = new Koa(); + + app.use((ctx, next) => { + ctx.state = { token: 'DONT_CLOBBER_ME' }; + return next(); + }); + app.use(koajwt({ secret: secret, key: 'jwtdata' })); + app.use(ctx => { + ctx.body = { token: ctx.state.token }; + }); + + request(app.listen()) + .get('/') + .set('Authorization', 'Bearer ' + token) + .expect(200) + .expect(validUserResponse) + .end(done); + }); it('should populate the raw token to ctx.state, in key from opts.tokenKey', done => { const validUserResponse = res => res.body.token !== token && "Token not passed through";