Created
May 17, 2013 16:04
-
-
Save sivagao/5600094 to your computer and use it in GitHub Desktop.
nodejs: everyauth nodejs.js
This file contains hidden or 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
| var express = require('express') | |
| , everyauth = require('../index') | |
| , conf = require('./conf') | |
| , everyauthRoot = __dirname + '/..'; | |
| 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 usersByWeiboId = {}; | |
| var usersByLogin = { | |
| 'brian@example.com': addUser({ login: 'brian@example.com', password: 'password'}) | |
| }; | |
| everyauth.everymodule | |
| .findUserById( function (id, callback) { | |
| callback(null, usersById[id]); | |
| }); | |
| everyauth | |
| .consumerKey(conf.twit.consumerKey) | |
| .consumerSecret(conf.twit.consumerSecret) | |
| .findOrCreateUser( function (sess, accessToken, accessSecret, twitUser) { | |
| return usersByTwitId[twitUser.id] || (usersByTwitId[twitUser.id] = addUser('twitter', twitUser)); | |
| }) | |
| .redirectPath('/'); | |
| everyauth | |
| .password | |
| .loginWith('email') | |
| .getLoginPath('/login') | |
| .postLoginPath('/login') | |
| .loginView('login.jade') | |
| .loginLocals( function (req, res, done) { | |
| setTimeout( function () { | |
| done(null, { | |
| title: 'Async login' | |
| }); | |
| }, 200); | |
| }) | |
| .authenticate( function (login, password) { | |
| var errors = []; | |
| if (!login) errors.push('Missing login'); | |
| if (!password) errors.push('Missing password'); | |
| if (errors.length) return errors; | |
| var user = usersByLogin[login]; | |
| if (!user) return ['Login failed']; | |
| if (user.password !== password) return ['Login failed']; | |
| return user; | |
| }) | |
| .getRegisterPath('/register') | |
| .postRegisterPath('/register') | |
| .registerView('register.jade') | |
| .registerLocals( function (req, res, done) { | |
| setTimeout( function () { | |
| done(null, { | |
| title: 'Async Register' | |
| }); | |
| }, 200); | |
| }) | |
| .validateRegistration( function (newUserAttrs, errors) { | |
| var login = newUserAttrs.login; | |
| if (usersByLogin[login]) errors.push('Login already taken'); | |
| return errors; | |
| }) | |
| .registerUser( function (newUserAttrs) { | |
| var login = newUserAttrs[this.loginKey()]; | |
| return usersByLogin[login] = addUser(newUserAttrs); | |
| }) | |
| .loginSuccessRedirect('/') | |
| .registerSuccessRedirect('/'); | |
| everyauth | |
| .github | |
| .appId(conf.github.appId) | |
| .appSecret(conf.github.appSecret) | |
| .findOrCreateUser( function (sess, accessToken, accessTokenExtra, ghUser) { | |
| return usersByGhId[ghUser.id] || (usersByGhId[ghUser.id] = addUser('github', ghUser)); | |
| }) | |
| .redirectPath('/'); | |
| everyauth | |
| .appId(conf.weibo.appId) | |
| .appSecret(conf.weibo.appSecret) | |
| .findOrCreateUser( function (session, accessToken, accessTokenExtra, weiboUser){ | |
| return usersByWeiboId[weiboUser.uid] || | |
| (usersByWeiboId[weiboUser.uid] = addUser('weibo', weiboUser)); | |
| }) | |
| .redirectPath("/"); | |
| var app = express.createServer( | |
| express.bodyParser() | |
| , express.static(__dirname + "/public") | |
| , express.favicon() | |
| , express.cookieParser() | |
| , express.session({ secret: 'htuayreve'}) | |
| , everyauth.middleware() | |
| ); | |
| app.configure( function () { | |
| app.set('view engine', 'jade'); | |
| app.set('views', everyauthRoot + '/example/views'); | |
| }); | |
| app.get('/', function (req, res) { | |
| res.render('home'); | |
| }); | |
| app.listen(3000); | |
| console.log('Go to http://local.host:3000'); | |
| module.exports = app; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment