Skip to content

Commit

Permalink
Merge branch 'master' into rewrite
Browse files Browse the repository at this point in the history
Conflicts:
	lib/modules/everymodule.js
  • Loading branch information
bnoguchi committed Mar 6, 2012
2 parents 4a935ed + 2dea084 commit 16590b7
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 43 deletions.
24 changes: 17 additions & 7 deletions README.md
Expand Up @@ -268,29 +268,39 @@ everyauth.everymodule.handleLogout( function (req, res) {


// And/or put your extra logic here // And/or put your extra logic here


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


## Custom redirect on login or registration ## 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: 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 everyauth.password
.respondToLoginSucceed( function (res, user, data) { .respondToLoginSucceed( function (res, user, data) {
if (user) { if (user) {
res.writeHead(303, {'Location': data.session.redirectTo}); this.redirect(res, data.session.redirectTo)
res.end();
} }
}) })
.respondToRegistrationSucceed( function (res, user, data) { .respondToRegistrationSucceed( function (res, user, data) {
res.writeHead(303, {'Location': data.session.redirectTo}); this.redirect(res, data.session.redirectTo)
res.end();
}) })
``` ```


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 # Auth Strategy Instructions


## Facebook Connect ## Facebook Connect
Expand Down
9 changes: 3 additions & 6 deletions lib/modules/azureacs.js
Expand Up @@ -79,9 +79,7 @@ everyModule.submodule('azureacs')


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

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


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


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


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


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


.entryPath('/auth/box') .entryPath('/auth/box')
Expand Down
13 changes: 11 additions & 2 deletions lib/modules/everymodule.js
Expand Up @@ -253,6 +253,10 @@ var everyModule = module.exports = {
seq.initialArgs = args; seq.initialArgs = args;
throw seq; throw seq;
} }

, redirect: function (req, location) {
this._performRedirect(req, location);
}
}; };


// Inherit from EventEmitter // Inherit from EventEmitter
Expand Down Expand Up @@ -333,6 +337,7 @@ everyModule
'defaults to `throw` wrapper' 'defaults to `throw` wrapper'
, logoutRedirectPath: 'Where to redirect the app upon logging out' , 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)' , 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') .get('logoutPath')
.step('handleLogout') .step('handleLogout')
Expand All @@ -341,11 +346,15 @@ everyModule
.logoutPath('/logout') .logoutPath('/logout')
.handleLogout( function (req, res) { .handleLogout( function (req, res) {
req.logout(); req.logout();
res.writeHead(303, { 'Location': this.logoutRedirectPath() }); this.redirect(res, this.logoutRedirectPath());
res.end();
}) })
.logoutRedirectPath('/'); .logoutRedirectPath('/');


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

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


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


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


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


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


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


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


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


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

0 comments on commit 16590b7

Please sign in to comment.