Skip to content

Commit

Permalink
middleware/token: store the token in current ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav Bajtoš committed Nov 10, 2014
1 parent dd743d9 commit dfa296b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/middleware/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ function token(options) {
if (req.accessToken !== undefined) return next();
TokenModel.findForRequest(req, options, function(err, token) {
req.accessToken = token || null;
var ctx = loopback.getCurrentContext();
if (ctx) ctx.set('accessToken', token);
next(err);
});
};
Expand Down
30 changes: 30 additions & 0 deletions test/access-token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@ describe('app.enableAuth()', function() {
.end(done);
});

it('stores token in the context', function(done) {
var TestModel = loopback.createModel('TestModel', { base: 'Model' });
TestModel.getToken = function(cb) {
cb(null, loopback.getCurrentContext().get('accessToken') || null);
};
TestModel.remoteMethod('getToken', {
returns: { arg: 'token', type: 'object' },
http: { verb: 'GET', path: '/token' }
});

var app = loopback();
app.model(TestModel, { dataSource: null });

app.enableAuth();
app.use(loopback.context());
app.use(loopback.token({ model: Token }));
app.use(loopback.rest());

var token = this.token;
request(app)
.get('/TestModels/token?_format=json')
.set('authorization', token.id)
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) return done(err);
expect(res.body.token.id).to.eql(token.id);
done();
});
});
});

function createTestingToken(done) {
Expand Down

0 comments on commit dfa296b

Please sign in to comment.