Skip to content

Commit

Permalink
fix: should cleanup userauthLoginReferer on session (#34)
Browse files Browse the repository at this point in the history
pick from #33
  • Loading branch information
fengmk2 committed Apr 12, 2018
1 parent 860dac2 commit 229035b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -308,9 +308,11 @@ function login(options) {
*/

function loginCallback(options) {
return function *loginCallbackHandler() {
return function* loginCallbackHandler() {
var referer = this.session.userauthLoginReferer || options.rootPath;
debug('loginReferer in session: %j', this.session.userauthLoginReferer);
debug('[loginCallbackHandler] loginReferer in session: %j', this.session.userauthLoginReferer);
// cleanup the userauthLoginReferer on session
this.session.userauthLoginReferer = undefined;
var user = this.session[options.userField];
if (user) {
// already login
Expand Down
3 changes: 2 additions & 1 deletion test/app.js
Expand Up @@ -90,7 +90,8 @@ module.exports = function(match, ignore) {
app.use(function* (next) {
this.body = {
user: this.session.user || null,
message: this.method + ' ' + this.url
message: this.method + ' ' + this.url,
userauthLoginReferer: this.session.userauthLoginReferer,
};
});
return app.callback();
Expand Down
45 changes: 26 additions & 19 deletions test/index.test.js
Expand Up @@ -421,35 +421,42 @@ describe('test/index.test.js', function () {

it('should return 200 status and user info after user logined', function (done) {
request(app)
.get('/login/callback')
.set('mocklogin', 1)
.expect('Location', '/')
.get('/login?redirect=%2Fuser%2Ffoo')
.expect(302, function (err, res) {
should.not.exist(err);
var cookie = res.headers['set-cookie'].join(';');

request(app)
.get('/')
.get('/login/callback')
.set('mocklogin', 1)
.set({ Cookie: 'cookie2=1234; ' + cookie })
.expect({
user: {
nick: 'mock user',
userid: 1234
},
message: 'GET /'
})
.expect(200, function (err, res) {
// logout
.expect('Location', '/user/foo')
.expect(302, function (err, res) {
should.not.exist(err);
request(app)
.get('/logout')
.get('/user/foo')
.set({ Cookie: 'cookie2=1234; ' + cookie })
.expect('Location', '/')
.expect(302, function () {
.expect({
user: {
nick: 'mock user',
userid: 1234
},
message: 'GET /user/foo'
})
.expect(200, function (err, res) {
// logout
should.not.exist(err);
request(app)
.get('/logout')
.set({ referer: '/login' })
.expect('Location', '/login')
.expect(302, done);
.set({ Cookie: 'cookie2=1234; ' + cookie })
.expect('Location', '/')
.expect(302, function () {
request(app)
.get('/logout')
.set({ referer: '/login' })
.expect('Location', '/login')
.expect(302, done);
});
});
});
});
Expand Down

0 comments on commit 229035b

Please sign in to comment.