Skip to content

Commit 2815dc9

Browse files
authored
Merge pull request #1012 from jaredhanson/authinfo-assignprop
Set req.authInfo when using assignProperty option
2 parents cfdbd4a + 0f2f81c commit 2815dc9

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

lib/middleware/authenticate.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,16 @@ module.exports = function authenticate(passport, name, options, callback) {
250250
}
251251
if (options.assignProperty) {
252252
req[options.assignProperty] = user;
253-
return next();
253+
if (options.authInfo !== false) {
254+
passport.transformAuthInfo(info, req, function(err, tinfo) {
255+
if (err) { return next(err); }
256+
req.authInfo = tinfo;
257+
next();
258+
});
259+
} else {
260+
next();
261+
}
262+
return;
254263
}
255264

256265
req.logIn(user, options, function(err) {

test/authenticator.middleware.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,55 @@ describe('Authenticator', function() {
245245
expect(request.account.username).to.equal('jaredhanson');
246246
});
247247

248+
it('should set authInfo to empty object', function() {
249+
expect(request.authInfo).to.deep.equal({});
250+
});
251+
});
252+
253+
describe('handling a request with authInfo disabled', function() {
254+
function Strategy() {
255+
}
256+
Strategy.prototype.authenticate = function(req) {
257+
var user = { id: '1', username: 'jaredhanson' };
258+
this.success(user);
259+
};
260+
261+
var passport = new Authenticator();
262+
passport.use('success', new Strategy());
263+
264+
var request, error;
265+
266+
before(function(done) {
267+
chai.connect.use(passport.authorize('success', { authInfo: false }))
268+
.req(function(req) {
269+
request = req;
270+
271+
req.logIn = function(user, options, done) {
272+
this.user = user;
273+
done();
274+
};
275+
})
276+
.next(function(err) {
277+
error = err;
278+
done();
279+
})
280+
.dispatch();
281+
});
282+
283+
it('should not error', function() {
284+
expect(error).to.be.undefined;
285+
});
286+
287+
it('should not set user', function() {
288+
expect(request.user).to.be.undefined;
289+
});
290+
291+
it('should set account', function() {
292+
expect(request.account).to.be.an('object');
293+
expect(request.account.id).to.equal('1');
294+
expect(request.account.username).to.equal('jaredhanson');
295+
});
296+
248297
it('should not set authInfo', function() {
249298
expect(request.authInfo).to.be.undefined;
250299
});

test/middleware/authenticate.success.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,55 @@ describe('middleware/authenticate', function() {
9898
expect(request.account.username).to.equal('jaredhanson');
9999
});
100100

101+
it('should set authInfo to empty object', function() {
102+
expect(request.authInfo).to.deep.equal({});
103+
});
104+
});
105+
106+
describe('success that assigns a specific property with authInfo disabled', function() {
107+
function Strategy() {
108+
}
109+
Strategy.prototype.authenticate = function(req) {
110+
var user = { id: '1', username: 'jaredhanson' };
111+
this.success(user);
112+
};
113+
114+
var passport = new Passport();
115+
passport.use('success', new Strategy());
116+
117+
var request, error;
118+
119+
before(function(done) {
120+
chai.connect.use(authenticate(passport, 'success', { assignProperty: 'account', authInfo: false }))
121+
.req(function(req) {
122+
request = req;
123+
124+
req.logIn = function(user, options, done) {
125+
this.user = user;
126+
done();
127+
};
128+
})
129+
.next(function(err) {
130+
error = err;
131+
done();
132+
})
133+
.dispatch();
134+
});
135+
136+
it('should not error', function() {
137+
expect(error).to.be.undefined;
138+
});
139+
140+
it('should not set user', function() {
141+
expect(request.user).to.be.undefined;
142+
});
143+
144+
it('should set account', function() {
145+
expect(request.account).to.be.an('object');
146+
expect(request.account.id).to.equal('1');
147+
expect(request.account.username).to.equal('jaredhanson');
148+
});
149+
101150
it('should not set authInfo', function() {
102151
expect(request.authInfo).to.be.undefined;
103152
});

0 commit comments

Comments
 (0)