Skip to content

Commit

Permalink
Add everymodule.performRedirect configurable method
Browse files Browse the repository at this point in the history
It is by default implemented as
    res.writeHead(303, { 'Location': location });
    res.end();

Defining a single redirect function that all modules use allows for
centralized redirect mapping. Specifically express users may want to
deploy res.redirect to implement successful login redirection.

this.redirect is now used in all modules: it calls _performRedirect
method configured for each module.
  • Loading branch information
pirxpilot committed Mar 1, 2012
1 parent 4fc029f commit 1c6efa9
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 43 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,29 +268,39 @@ everyauth.everymodule.handleLogout( function (req, res) {
// And/or put your extra logic here
res.writeHead(303, { 'Location': this.logoutRedirectPath() });
res.end();
this.redirect(res, this.logoutRedirectPath());
});
```

## Custom redirect on login or registration

You may want your own callback that decides where to send a user after login or registration. One way of doing this is with the `respondToLoginSucceed` and `respondToRegistrationSucceed` methods. This assumes that you have set a `.redirectTo` property on your `req.session` object:

```
```javascript
everyauth.password
.respondToLoginSucceed( function (res, user, data) {
if (user) {
res.writeHead(303, {'Location': data.session.redirectTo});
res.end();
this.redirect(res, data.session.redirectTo)
}
})
.respondToRegistrationSucceed( function (res, user, data) {
res.writeHead(303, {'Location': data.session.redirectTo});
res.end();
this.redirect(res, data.session.redirectTo)
})
```

If you are using express and want your redirects to be subject to [express
redirect mapping](http://expressjs.com/guide.html#res.redirect\(\)), you can
overwrite redirect method employed by everyauth.

```javascript
everyauth.everymodule
.performRedirect( function (res, location) {
res.redirect(location, 303);
});
```

A newly defined method will be used by everyauth to perform all redirects.

# Auth Strategy Instructions

## Facebook Connect
Expand Down
9 changes: 3 additions & 6 deletions lib/modules/azureacs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ everyModule.submodule('azureacs')

.redirectToIdentityProviderSelector( function (req, res) {
var identityProviderSelectorUri = this.wsfederation.getRequestSecurityTokenUrl();

res.writeHead(303, {'Location': identityProviderSelectorUri});
res.end();
this.redirect(res, identityProviderSelectorUri);
})

.getToken( function (req, res) {
Expand Down Expand Up @@ -131,15 +129,14 @@ everyModule.submodule('azureacs')
_auth.loggedIn = true;
_auth.userId || (_auth.userId = acsUser.id);
mod.user = acsUser;
mod.accessToken = token;
mod.accessToken = token;
})

.sendResponse( function (res) {
var redirectTo = this.redirectPath();
if (!redirectTo)
throw new Error('You must configure a redirectPath');
res.writeHead(303, {'Location': redirectTo});
res.end();
this.redirect(res, redirectTo);
})

.authCallbackDidErr( function (req) {
Expand Down
8 changes: 2 additions & 6 deletions lib/modules/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ everyModule.submodule('box')
return promise;
})
.redirectToBoxAuth( function (res, ticket) {
res.writeHead(303, {
'Location': this._apiHost + '/auth/' + ticket
});
res.end();
this.redirect(res, this._apiHost + '/auth/' + ticket);
})

.extractAuthToken( function (req, res) {
Expand Down Expand Up @@ -121,8 +118,7 @@ everyModule.submodule('box')
var redirectTo = this.redirectPath();
if (!redirectTo)
throw new Error('You must configure a redirectPath');
res.writeHead(303, {'Location': redirectTo});
res.end();
this.redirect(res, redirectTo);
})

.entryPath('/auth/box')
Expand Down
12 changes: 10 additions & 2 deletions lib/modules/everymodule.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ var everyModule = module.exports = {
}
, get: route('get')
, post: route('post')
, redirect: function (req, location) {
this._performRedirect(req, location);
}
, stepseq: function (name, description) {
this.configurable(name, description);
this._currSeq =
Expand Down Expand Up @@ -334,6 +337,7 @@ everyModule
'any time an error occurs in the module; defaults to `throw` wrapper'
, logoutRedirectPath: 'where to redirect the app upon logging out'
, findUserById: 'function for fetching a user by his/her id -- used to assign to `req.user` - function (userId, callback) where function callback (err, user)'
, performRedirect: 'function for redirecting responses'
})
.get('logoutPath')
.step('handleLogout')
Expand All @@ -342,11 +346,15 @@ everyModule
.logoutPath('/logout')
.handleLogout( function (req, res) {
req.logout();
res.writeHead(303, { 'Location': this.logoutRedirectPath() });
res.end();
this.redirect(res, this.logoutRedirectPath());
})
.logoutRedirectPath('/');

everyModule.performRedirect( function(res, location) {
res.writeHead(303, { 'Location': location });
res.end();
});

everyModule.moduleTimeout(10000);
everyModule.moduleErrback( function (err) {
if (! (err instanceof Error)) {
Expand Down
3 changes: 1 addition & 2 deletions lib/modules/googlehybrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ openidModule.submodule('googlehybrid')

this.relyingParty.authenticate('http://www.google.com/accounts/o8/id', false, function (err,authenticationUrl){
if(err) return p.fail(err);
res.writeHead(302, { Location: authenticationUrl });
res.end();
this.redirect(res, authenticationUrl);
});
})
.entryPath('/auth/googlehybrid')
Expand Down
3 changes: 1 addition & 2 deletions lib/modules/linkedin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ oauthModule.submodule('linkedin')
.callbackPath('/auth/linkedin/callback')

.redirectToProviderAuth( function (res, token) {
res.writeHead(303, { 'Location': 'https://www.linkedin.com' + this.authorizePath() + '?oauth_token=' + token });
res.end();
this.redirect(res, 'https://www.linkedin.com' + this.authorizePath() + '?oauth_token=' + token);
})

.fetchOAuthUser( function (accessToken, accessTokenSecret, params) {
Expand Down
6 changes: 2 additions & 4 deletions lib/modules/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ everyModule.submodule('oauth')
if (this._sendCallbackWithAuthorize) {
redirectTo += '&oauth_callback=' + this._myHostname + this._callbackPath;
}
res.writeHead(303, { 'Location': redirectTo });
res.end();
this.redirect(res, redirectTo);
})

// Steps for GET `callbackPath`
Expand Down Expand Up @@ -200,8 +199,7 @@ everyModule.submodule('oauth')
var redirectTo = this.redirectPath();
if (!redirectTo)
throw new Error('You must configure a redirectPath');
res.writeHead(303, {'Location': redirectTo});
res.end();
this.redirect(res, redirectTo);
})

.waitForPriorRequestToWriteSession( function (req, res) {
Expand Down
6 changes: 2 additions & 4 deletions lib/modules/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ everyModule.submodule('oauth2')
return url + '?' + querystring.stringify(params);
})
.requestAuthUri( function (res, authUri) {
res.writeHead(303, {'Location': authUri});
res.end();
this.redirect(res, authUri);
})
.getCode( function (req, res) {
var parsedUrl = url.parse(req.url, true);
Expand Down Expand Up @@ -222,8 +221,7 @@ everyModule.submodule('oauth2')
var redirectTo = this.redirectPath();
if (!redirectTo)
throw new Error('You must configure a redirectPath');
res.writeHead(303, {'Location': redirectTo});
res.end();
this.redirect(res, redirectTo);
})

.authCallbackDidErr( function (req, res) {
Expand Down
6 changes: 2 additions & 4 deletions lib/modules/openid.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ everyModule.submodule('openid')

this.relyingParty.authenticate(req.query[this.openidURLField()], false, function(err,authenticationUrl){
if(err) return p.fail(err);
res.writeHead(302, { Location: authenticationUrl });
res.end();
this.redirect(res, authenticationUrl);
});
})
.getSession( function(req) {
Expand All @@ -79,8 +78,7 @@ everyModule.submodule('openid')
var redirectTo = this.redirectPath();
if (!redirectTo)
throw new Error('You must configure a redirectPath');
res.writeHead(303, {'Location': redirectTo});
res.end();
this.redirect(res, redirectTo);
})
.redirectPath('/')
.entryPath('/auth/openid')
Expand Down
6 changes: 2 additions & 4 deletions lib/modules/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ everyModule.submodule('password')
})
.respondToLoginSucceed( function (res, user) {
if (user) {
res.writeHead(303, {'Location': this.loginSuccessRedirect()});
res.end();
this.redirect(res, this.loginSuccessRedirect());
}
})
.respondToLoginFail( function (req, res, errors, login) {
Expand Down Expand Up @@ -327,8 +326,7 @@ everyModule.submodule('password')
return user;
})
.respondToRegistrationSucceed( function (res, user) {
res.writeHead(303, {'Location': this.registerSuccessRedirect()});
res.end();
this.redirect(res, this.registerSuccessRedirect());
})

.stepseq('registrationFailSteps')
Expand Down
3 changes: 1 addition & 2 deletions lib/modules/tripit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ oauthModule.submodule('tripit')
if (this.sendCallbackWithAuthorize()) {
redirectTo += '&oauth_callback=' + this.myHostname() + this.callbackPath();
}
res.writeHead(303, { 'Location': redirectTo });
res.end();
this.redirect(res, redirectTo);
})
.fetchOAuthUser( function (accessToken, accessTokenSecret, params) {
var promise = this.Promise();
Expand Down

0 comments on commit 1c6efa9

Please sign in to comment.