Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

postLoginHandler for OAuth2 Password Grant #510

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/controllers/get-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ var stormpath = require('stormpath');
*/
module.exports = function (req, res) {
var application = req.app.get('stormpathApplication');
var client = req.app.get('stormpathClient');
var config = req.app.get('stormpathConfig');
var grantType = req.body.grant_type;
var isPostRequest = req.method === 'POST';
var logger = req.app.get('stormpathLogger');
var loginHandler = config.postLoginHandler;

function writeErrorResponse(err) {
var error = {
Expand All @@ -42,6 +44,29 @@ module.exports = function (req, res) {
});
break;
case 'password':
var authenticator = new stormpath.OAuthAuthenticator(application);

authenticator.authenticate(req, function (err, authResult) {
if (err) {
return writeErrorResponse(err);
}

if (loginHandler) {
client.getAccount(authResult.account.href, function (err, account) {
if (err) {
return writeErrorResponse(err);
}

loginHandler(account, req, res, function () {
res.json(authResult.accessTokenResponse);
});
});
} else {
res.json(authResult.accessTokenResponse);
}
});
break;

case 'refresh_token':
var authenticator = new stormpath.OAuthAuthenticator(application);

Expand Down