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

Commit

Permalink
fix login/registration handlers for oauth provider login
Browse files Browse the repository at this point in the history
  • Loading branch information
typerandom committed Jul 10, 2016
1 parent f1abe55 commit 7afa483
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions lib/helpers/login-with-oauth-provider.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';

var exchangeStormpathToken = require('./exchange-stormpath-token');
var loginResponder = require('./login-responder');
var oauth = require('../oauth');
var writeJsonError = require('./write-json-error');
var loginResponder = require('./login-responder');
var exchangeStormpathToken = require('./exchange-stormpath-token');

/**
* loginWithOAuthProvider takes provider data, such as an access token,
Expand All @@ -16,7 +15,12 @@ var writeJsonError = require('./write-json-error');
* @param {Object} res - The http response.
*/
module.exports = function loginWithOAuthProvider(options, req, res) {
var config = req.app.get('stormpathConfig');
var application = req.app.get('stormpathApplication');
var preLoginHandler = config.preLoginHandler;
var postLoginHandler = config.postLoginHandler;
var preRegistrationHandler = config.preRegistrationHandler;
var postRegistrationHandler = config.postRegistrationHandler;

application.getAccount(options, function (err, providerAccountResult) {
if (err) {
Expand All @@ -25,12 +29,44 @@ module.exports = function loginWithOAuthProvider(options, req, res) {

var account = providerAccountResult.account;

exchangeStormpathToken(req, account, function (err, authenticationResult) {
if (err) {
return writeJsonError(res, err);
}
function continueWithTokenExchange() {
exchangeStormpathToken(req, account, function (err, authenticationResult) {
if (err) {
return oauth.errorResponder(req, res, err);
}

loginResponder(authenticationResult, account, req, res);
});
}

function continueWithHandlers(preHandler, postHandler) {
preHandler(options, req, res, function (err) {
if (err) {
return oauth.errorResponder(req, res, err);
}

if (postHandler) {
return postHandler(account, req, res, function (err) {
if (err) {
return oauth.errorResponder(req, res, err);
}

continueWithTokenExchange();
});
}

continueWithTokenExchange();
});
}

if (preRegistrationHandler && providerAccountResult.created) {
return continueWithHandlers(preRegistrationHandler, postRegistrationHandler);
}

if (preLoginHandler && !providerAccountResult.created) {
return continueWithHandlers(preLoginHandler, postLoginHandler);
}

loginResponder(authenticationResult, account, req, res);
});
continueWithTokenExchange();
});
};

0 comments on commit 7afa483

Please sign in to comment.