diff --git a/lib/controllers/get-token.js b/lib/controllers/get-token.js index 490b5f02..7480bdef 100644 --- a/lib/controllers/get-token.js +++ b/lib/controllers/get-token.js @@ -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 = { @@ -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);