forked from bnoguchi/mongoose-auth
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cronopio
authored and
Jim Lloyd
committed
Nov 25, 2011
1 parent
6a5666d
commit bbd5421
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Defaults | ||
module.exports = { | ||
findOrCreateUser: function (sess, accessTok, accessTokExtra, googleUser) { | ||
var promise = this.Promise() | ||
, User = this.User()(); | ||
// TODO Check user in session or request helper first | ||
// e.g., req.user or sess.auth.userId | ||
User.findOne({'google.id': googleUser.id}, function (err, foundUser) { | ||
if (foundUser) { | ||
return promise.fulfill(foundUser); | ||
} | ||
console.log("CREATING"); | ||
User.createWithGoogleOAuth(googleUser, accessTok, accessTokExtra, function (err, createdUser) { | ||
if (err) return promise.fail(err); | ||
return promise.fulfill(createdUser); | ||
}); | ||
}); | ||
return promise; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var mongoose = require('mongoose') | ||
, mongooseTypes = require('mongoose-types') | ||
, _schema = require('./schema') | ||
, everyauth = require('everyauth'); | ||
mongooseTypes.loadTypes(mongoose); | ||
|
||
module.exports = function google (schema, opts) { | ||
schema.add(_schema); | ||
|
||
schema.static('createWithGoogleOAuth', function (googleUser, accessToken, accessTokenExtra, callback) { | ||
var expiresDate = new Date; | ||
expiresDate.setSeconds(expiresDate.getSeconds() + accessTokenExtra.expires_in); | ||
|
||
var params = { | ||
google: { | ||
email: googleUser.email | ||
, expires: expiresDate | ||
, accessToken: accessToken | ||
, refreshToken: accessTokenExtra.refresh_token | ||
} | ||
}; | ||
|
||
// TODO Only do this if password module is enabled | ||
// Currently, this is not a valid way to check for enabled | ||
if (everyauth.password) | ||
params[everyauth.password.loginKey()] = "google:" + googleUser.id; // Hack because of way mongodb treate unique indexes | ||
|
||
this.create(params, callback); | ||
}); | ||
}; |