Browse files

Modified example app, so that it supports `req.user` -- now users are…

… stored by a user id.
  • Loading branch information...
1 parent 02a457e commit 5d936d1cfd2b42d7e4d494ce0fa482c3fb1117bf @bnoguchi committed Jun 29, 2011
Showing with 37 additions and 19 deletions.
  1. +37 −19 example/server.js
View
56 example/server.js
@@ -4,6 +4,22 @@ var express = require('express')
everyauth.debug = true;
+var usersById = {};
+var nextUserId = 0;
+
+function addUser (source, sourceUser) {
+ var user;
+ if (arguments.length === 1) { // password-based
+ user = sourceUser = source;
+ user.id = ++nextUserId;
+ return usersById[nextUserId] = user;
+ } else { // non-password-based
+ user = usersById[++nextUserId] = {id: nextUserId};
+ user[source] = sourceUser;
+ }
+ return user;
+}
+
var usersByVimeoId = {};
var usersByJustintvId = {};
var usersByDropboxId = {};
@@ -20,18 +36,20 @@ var usersByReadabilityId = {};
var usersByBoxId = {};
var usersByOpenId = {};
var usersByLogin = {
- 'brian': {
- login: 'brian'
- , password: 'password'
- }
+ 'brian': addUser({ login: 'brian', password: 'password'})
};
+everyauth.everymodule
+ .findUserById( function (id, callback) {
+ callback(null, usersById[id]);
+ });
+
everyauth
.openid
.myHostname('http://local.host:3000')
.findOrCreateUser( function (session, userMetadata) {
return usersByOpenId[userMetadata.claimedIdentifier] ||
- (usersByOpenId[userMetadata.claimedIdentifier] = userMetadata);
+ (usersByOpenId[userMetadata.claimedIdentifier] = addUser('openid', userMetadata));
})
.redirectPath('/');
@@ -42,7 +60,7 @@ everyauth
.appSecret(conf.fb.appSecret)
.findOrCreateUser( function (session, accessToken, accessTokenExtra, fbUserMetadata) {
return usersByFbId[fbUserMetadata.id] ||
- (usersByFbId[fbUserMetadata.id] = fbUserMetadata);
+ (usersByFbId[fbUserMetadata.id] = addUser('facebook', fbUserMetadata));
})
.redirectPath('/');
@@ -52,7 +70,7 @@ everyauth
.consumerKey(conf.twit.consumerKey)
.consumerSecret(conf.twit.consumerSecret)
.findOrCreateUser( function (sess, accessToken, accessSecret, twitUser) {
- return usersByTwitId[twitUser.id] || (usersByTwitId[twitUser.id] = twitUser);
+ return usersByTwitId[twitUser.id] || (usersByTwitId[twitUser.id] = addUser('twitter', twitUser));
})
.redirectPath('/');
@@ -113,7 +131,7 @@ everyauth
})
.registerUser( function (newUserAttrs) {
var login = newUserAttrs[this.loginKey()];
- return usersByLogin[login] = newUserAttrs;
+ return usersByLogin[login] = addUser(newUserAttrs);
})
.loginSuccessRedirect('/')
@@ -124,7 +142,7 @@ everyauth.github
.appId(conf.github.appId)
.appSecret(conf.github.appSecret)
.findOrCreateUser( function (sess, accessToken, accessTokenExtra, ghUser) {
- return usersByGhId[ghUser.id] || (usersByGhId[ghUser.id] = ghUser);
+ return usersByGhId[ghUser.id] || (usersByGhId[ghUser.id] = addUser('github', ghUser));
})
.redirectPath('/');
@@ -133,7 +151,7 @@ everyauth.instagram
.appId(conf.instagram.clientId)
.appSecret(conf.instagram.clientSecret)
.findOrCreateUser( function (sess, accessToken, accessTokenExtra, hipster) {
- return usersByInstagramId[hipster.id] || (usersByInstagramId[hipster.id] = hipster);
+ return usersByInstagramId[hipster.id] || (usersByInstagramId[hipster.id] = addUser('instagram', hipster));
})
.redirectPath('/');
@@ -142,7 +160,7 @@ everyauth.foursquare
.appId(conf.foursquare.clientId)
.appSecret(conf.foursquare.clientSecret)
.findOrCreateUser( function (sess, accessTok, accessTokExtra, addict) {
- return usersByFoursquareId[addict.id] || (usersByFoursquareId[addict.id] = addict);
+ return usersByFoursquareId[addict.id] || (usersByFoursquareId[addict.id] = addUser('foursquare', addict));
})
.redirectPath('/');
@@ -151,7 +169,7 @@ everyauth.linkedin
.consumerKey(conf.linkedin.apiKey)
.consumerSecret(conf.linkedin.apiSecret)
.findOrCreateUser( function (sess, accessToken, accessSecret, linkedinUser) {
- return usersByLinkedinId[linkedinUser.id] || (usersByLinkedinId[linkedinUser.id] = linkedinUser);
+ return usersByLinkedinId[linkedinUser.id] || (usersByLinkedinId[linkedinUser.id] = addUser('linkedin', linkedinUser));
})
.redirectPath('/');
@@ -163,7 +181,7 @@ everyauth.google
.findOrCreateUser( function (sess, accessToken, extra, googleUser) {
googleUser.refreshToken = extra.refresh_token;
googleUser.expiresIn = extra.expires_in;
- return usersByGoogleId[googleUser.id] || (usersByGoogleId[googleUser.id] = googleUser);
+ return usersByGoogleId[googleUser.id] || (usersByGoogleId[googleUser.id] = addUser('google', googleUser));
})
.redirectPath('/');
@@ -172,7 +190,7 @@ everyauth.yahoo
.consumerKey(conf.yahoo.consumerKey)
.consumerSecret(conf.yahoo.consumerSecret)
.findOrCreateUser( function (sess, accessToken, accessSecret, yahooUser) {
- return usersByYahooId[yahooUser.id] || (usersByYahooId[yahooUser.id] = yahooUser);
+ return usersByYahooId[yahooUser.id] || (usersByYahooId[yahooUser.id] = addUser('yahoo', yahooUser));
})
.redirectPath('/');
@@ -182,7 +200,7 @@ everyauth.googlehybrid
.consumerSecret(conf.google.clientSecret)
.scope(['http://docs.google.com/feeds/','http://spreadsheets.google.com/feeds/'])
.findOrCreateUser( function(session, userAttributes) {
- return usersByGoogleHybridId[userAttributes.claimedIdentifier] || (usersByGoogleHybridId[userAttributes.claimedIdentifier] = userAttributes);
+ return usersByGoogleHybridId[userAttributes.claimedIdentifier] || (usersByGoogleHybridId[userAttributes.claimedIdentifier] = addUser('googlehybrid', userAttributes));
})
.redirectPath('/')
@@ -191,7 +209,7 @@ everyauth.readability
.consumerKey(conf.readability.consumerKey)
.consumerSecret(conf.readability.consumerSecret)
.findOrCreateUser( function (sess, accessToken, accessSecret, reader) {
- return usersByReadabilityId[reader.username] || (usersByReadabilityId[reader.username] = reader);
+ return usersByReadabilityId[reader.username] || (usersByReadabilityId[reader.username] = addUser('readability', reader));
})
.redirectPath('/');
@@ -202,7 +220,7 @@ everyauth
.consumerSecret(conf.dropbox.consumerSecret)
.findOrCreateUser( function (sess, accessToken, accessSecret, dropboxUserMetadata) {
return usersByDropboxId[dropboxUserMetadata.uid] ||
- (usersByDropboxId[dropboxUserMetadata.uid] = dropboxUserMetadata);
+ (usersByDropboxId[dropboxUserMetadata.uid] = addUser('dropbox', dropboxUserMetadata));
})
.redirectPath('/')
@@ -220,15 +238,15 @@ everyauth.justintv
.consumerSecret(conf.justintv.consumerSecret)
.findOrCreateUser( function (sess, accessToken, accessSecret, justintvUser) {
return usersByJustintvId[justintvUser.id] ||
- (usersByJustintvId[justintvUser.id] = justintvUser);
+ (usersByJustintvId[justintvUser.id] = addUser('justintv', justintvUser));
})
.redirectPath('/')
everyauth.box
.apiKey(conf.box.apiKey)
.findOrCreateUser( function (sess, authToken, boxUser) {
return usersByBoxId[boxUser.user_id] ||
- (usersByDropboxId[boxUser.user_id] = boxUser);
+ (usersByDropboxId[boxUser.user_id] = addUser('box', boxUser));
})
.redirectPath('/');

0 comments on commit 5d936d1

Please sign in to comment.