-
-
Save teddychan/5369555 to your computer and use it in GitHub Desktop.
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
// db.js (just the User model) | |
var UserSchema = new Schema({ | |
// _id: String, | |
username: String, | |
name: String, | |
gravatar: String, | |
email: String, | |
provider: String, | |
services: String, | |
singlyId: String, | |
hashed_password: String, | |
salt: String, | |
singly: {} | |
}) | |
// passport.js (the whole file) | |
var mongoose = require('mongoose') | |
, LocalStrategy = require('passport-local').Strategy | |
, SinglyStrategy = require('passport-singly').Strategy | |
, User = mongoose.model('User') | |
module.exports = function (passport, config) { | |
// require('./initializer') | |
passport.serializeUser(function(user, done) { | |
console.log('serializeUser: ' + user._id) | |
done(null, user._id); | |
}); | |
passport.deserializeUser(function(id, done) { | |
User.findById(id, function(err, user){ | |
console.log(user) | |
if(!err) done(null, user); | |
else done(err, null) | |
}) | |
}); | |
// use local strategy | |
passport.use(new LocalStrategy({ | |
usernameField: 'email', | |
passwordField: 'password' | |
}, | |
function(email, password, done) { | |
User.findOne({ email: email }, function (err, user) { | |
if (err) { return done(err) } | |
if (!user) { | |
return done(null, false, { message: 'Unknown user' }) | |
} | |
if (!user.authenticate(password)) { | |
return done(null, false, { message: 'Invalid password' }) | |
} | |
return done(null, user) | |
}) | |
} | |
)) | |
// use singly strategy | |
passport.use(new SinglyStrategy({ | |
clientID: config.singly.clientID, | |
clientSecret: config.singly.clientSecret, | |
callbackURL: config.singly.callbackURL | |
}, | |
function(accessToken, refreshToken, profile, done) { | |
User.findOne({ 'singly.id': profile._id}, function (err, user) { | |
if (err) { return done(err) } | |
if (!user) { | |
user = new User({ | |
name: JSON.parse(profile._raw).name | |
, email: JSON.parse(profile._raw).email | |
, gravatar: JSON.parse(profile._raw).gravatar | |
, username: JSON.parse(profile._raw).handle | |
, provider: 'singly' | |
, singlyId: profile.username | |
, password: 'blank' | |
// , services: profile._raw | |
}) | |
user.save(function (err) { | |
if (err) console.log(err) | |
return done(err, user) | |
}) | |
} | |
else { | |
return done(err, user) | |
} | |
}) | |
} | |
)); | |
} | |
// my 2 Singly routes, which are in my routes.js file | |
app.get('/auth/singly/callback', passport.authenticate('singly', { failureRedirect: '/login' }), users.authCallback) | |
app.get('/auth/singly/:service', passport.authenticate('singly')); | |
// show.dust, rough sample of Dust (Linked in version) template page users are redirected to: | |
{>base/} | |
{<content} | |
{#user} | |
<h1 id="page-title">{name}'s Profile</h1> | |
<div>our user name should be: {name}</div> | |
<div>Pic: <img src="{gravatar}"/></div> | |
{/user} | |
{/content} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment