Skip to content

Commit

Permalink
configured redirection back to the original route upon successful log…
Browse files Browse the repository at this point in the history
…in for #8
  • Loading branch information
travi committed Mar 7, 2016
1 parent 45b615b commit 8270f82
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
14 changes: 11 additions & 3 deletions lib/auth/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ exports.register = function (server, options, next) {
if (!request.auth.isAuthenticated) {
reply(Boom.unauthorized(`Authentication failed due to: ${request.auth.error.message}`));
} else {
reply.view('login', {
profile: request.auth.credentials.profile
});
const originalRoute = request.auth.credentials.query.next;

request.cookieAuth.set(request.auth.credentials);

if (originalRoute) {
reply.redirect(originalRoute);
} else {
reply.view('login', {
profile: request.auth.credentials.profile
});
}
}
}
});
Expand Down
49 changes: 43 additions & 6 deletions test/unit/auth/routes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ suite('auth routes', () => {
profile = any.simpleObject(),
server = {
route: sinon.stub()
},
request = {
auth: {
isAuthenticated: true,
credentials: {
profile,
query: {}
}
},
cookieAuth: {
set: sinon.spy()
}
};
server.route.withArgs(sinon.match({path: '/login'})).yieldsTo('handler', {
auth: {
isAuthenticated: true,
credentials: {profile}
}
}, reply);
server.route.withArgs(sinon.match({path: '/login'})).yieldsTo('handler', request, reply);

routes.register(server, null, next);

Expand All @@ -48,6 +55,7 @@ suite('auth routes', () => {
auth: 'auth0'
}
}));
assert.calledWith(request.cookieAuth.set, request.auth.credentials);
assert.calledWith(reply.view, 'login', {profile});
assert.calledOnce(next);
});
Expand All @@ -73,6 +81,35 @@ suite('auth routes', () => {
assert.calledWith(reply, error);
});

test('that request is redirected to original route when login not loaded directly', () => {
const
originalRoute = any.string(),
reply = {redirect: sinon.spy()},
profile = any.simpleObject(),
server = {
route: sinon.stub()
},
request = {
auth: {
isAuthenticated: true,
credentials: {
profile,
query: {
next: originalRoute
}
}
},
cookieAuth: {
set: sinon.spy()
}
};
server.route.withArgs(sinon.match({path: '/login'})).yieldsTo('handler', request, reply);

routes.register(server, null, sinon.spy());

assert.calledOnce(reply.redirect, originalRoute);
});

test('that the scopes route is defined', () => {
const
next = sinon.spy(),
Expand Down

0 comments on commit 8270f82

Please sign in to comment.