Handle oauth 1 & 2 errors in common spot

This commit is contained in:
Mike Bannister
2012-08-21 03:35:43 -04:00
committed by Nick Martin
parent 8fa9ff0114
commit 4bb3072361
3 changed files with 33 additions and 33 deletions

View File

@@ -76,7 +76,6 @@
// Skip everything if there's no service set by the oauth middleware
// XXX should we instead throw an error?
// XXX we should catch all exceptions here as we do in oauth2_server.js
if (!service) {
next();
return;
@@ -85,12 +84,27 @@
// Make sure we're configured
ensureConfigured(serviceName);
if (service.version === 1)
Meteor.accounts.oauth1._handleRequest(service, req.query, res);
else if (service.version === 2)
Meteor.accounts.oauth2._handleRequest(service, req.query, res);
else
throw new Error("Unexpected OAuth version " + service.version);
try {
if (service.version === 1)
Meteor.accounts.oauth1._handleRequest(service, req.query, res);
else if (service.version === 2)
Meteor.accounts.oauth2._handleRequest(service, req.query, res);
else
throw new Error("Unexpected OAuth version " + service.version);
} catch (err) {
// if we got thrown an error, save it off, it will get passed to
// the approporiate login call (if any) and reported there.
//
// The other option would be to display it in the popup tab that
// is still open at this point, ignoring the 'close' or 'redirect'
// we were passed. But then the developer wouldn't be able to
// style the error or react to it in any way.
if (req.query.state && err instanceof Error)
Meteor.accounts.oauth._loginResultForState[req.query.state] = err;
// also log to the server console, so the developer sees it.
Meteor._debug("Exception in oauth" + service.version + " handler", err);
}
};
// Handle _oauth paths, gets a bunch of stuff ready for the oauth implementation middleware

View File

@@ -41,7 +41,8 @@
// Get or create user id
var oauthResult = service.handleOauthRequest(oauth);
var userId = Meteor.accounts.updateOrCreateUser(oauthResult.options, oauthResult.extra);
var userId = Meteor.accounts.updateOrCreateUser(
oauthResult.options, oauthResult.extra);
// Generate and store a login token for reconnect
// XXX this could go in accounts_server.js instead

View File

@@ -12,34 +12,19 @@
// This way the subsequent call to the `login` method will be
// immediate.
try {
// Get or create user id
var oauthResult = service.handleOauthRequest(query);
// Get or create user id
var oauthResult = service.handleOauthRequest(query);
var userId = Meteor.accounts.updateOrCreateUser(
oauthResult.options, oauthResult.extra);
var userId = Meteor.accounts.updateOrCreateUser(
oauthResult.options, oauthResult.extra);
// Generate and store a login token for reconnect
// XXX this could go in accounts_server.js instead
var loginToken = Meteor.accounts._loginTokens.insert({userId: userId});
// Generate and store a login token for reconnect
// XXX this could go in accounts_server.js instead
var loginToken = Meteor.accounts._loginTokens.insert({userId: userId});
// Store results to subsequent call to `login`
Meteor.accounts.oauth._loginResultForState[query.state] =
{token: loginToken, id: userId};
} catch (err) {
// if we got thrown an error, save it off, it will get passed to
// the approporiate login call (if any) and reported there.
//
// The other option would be to display it in the popup tab that
// is still open at this point, ignoring the 'close' or 'redirect'
// we were passed. But then the developer wouldn't be able to
// style the error or react to it in any way.
if (query.state && err instanceof Error)
Meteor.accounts.oauth._loginResultForState[query.state] = err;
// also log to the server console, so the developer sees it.
Meteor._debug("Exception in oauth2 handler", err);
}
// Store results to subsequent call to `login`
Meteor.accounts.oauth._loginResultForState[query.state] =
{token: loginToken, id: userId};
// XXX push down to oauth_server.js?