Skip to content

Commit

Permalink
Initial propose for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
cronopio authored and Jim Lloyd committed Nov 25, 2011
1 parent 6a5666d commit bbd5421
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/modules/google/everyauth.js
@@ -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;
}
};
30 changes: 30 additions & 0 deletions lib/modules/google/plugin.js
@@ -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);
});
};

0 comments on commit bbd5421

Please sign in to comment.