mirror of
https://github.com/wassname/HackFlowy.git
synced 2026-08-15 12:04:59 +08:00
Auth- Added Back-end Logic
removed personal details
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
'googleAuth' : {
|
||||
'clientID' : 'your-secret-clientID-here',
|
||||
'clientSecret' : 'your-client-secret-here',
|
||||
'callbackURL' : 'http://localhost:3000/auth/google/callback'
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
var mongoose = require('mongoose');
|
||||
|
||||
// Connect to cloud database
|
||||
//https://mongolab.com/
|
||||
var username = "throwaway"
|
||||
var password = "throwaway1";
|
||||
var address = '@ds037637.mongolab.com:37637/throwaway_db';
|
||||
var url = 'mongodb://' + username + ':' + password + address;
|
||||
|
||||
function safeConnect() {
|
||||
try { mongoose.connect(url); }
|
||||
catch(err) { console.log("Error: Sign In to MongoLab") }
|
||||
// console.log("error caught");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
// config/passport.js
|
||||
|
||||
// load all the things we need
|
||||
// var LocalStrategy = require('passport-local').Strategy;
|
||||
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
|
||||
|
||||
// load up the user model
|
||||
var User = require('../models/user');
|
||||
|
||||
// load the auth variables
|
||||
var configAuth = require('./auth');
|
||||
|
||||
module.exports = function(passport) {
|
||||
|
||||
// used to serialize the user for the session
|
||||
passport.serializeUser(function(user, done) {
|
||||
done(null, user.id);
|
||||
});
|
||||
|
||||
// used to deserialize the user
|
||||
passport.deserializeUser(function(id, done) {
|
||||
User.findById(id, function(err, user) {
|
||||
done(err, user);
|
||||
});
|
||||
});
|
||||
|
||||
// code for login (use('local-login', new LocalStategy))
|
||||
// code for signup (use('local-signup', new LocalStategy))
|
||||
// code for facebook (use('facebook', new FacebookStrategy))
|
||||
// code for twitter (use('twitter', new TwitterStrategy))
|
||||
|
||||
// =========================================================================
|
||||
// GOOGLE ==================================================================
|
||||
// =========================================================================
|
||||
passport.use(new GoogleStrategy({
|
||||
|
||||
clientID : configAuth.googleAuth.clientID,
|
||||
clientSecret : configAuth.googleAuth.clientSecret,
|
||||
callbackURL : configAuth.googleAuth.callbackURL,
|
||||
|
||||
},
|
||||
function(token, refreshToken, profile, done) {
|
||||
|
||||
// make the code asynchronous
|
||||
// User.findOne won't fire until we have all our data back from Google
|
||||
process.nextTick(function() {
|
||||
|
||||
// try to find the user based on their google id
|
||||
User.findOne({ 'google.id' : profile.id }, function(err, user) {
|
||||
if (err)
|
||||
return done(err);
|
||||
|
||||
if (user) { // if a user is found, log them in
|
||||
return done(null, user);
|
||||
}
|
||||
|
||||
else {
|
||||
// if the user isnt in our database, create a new user
|
||||
var newUser = new User();
|
||||
|
||||
// set all of the relevant information
|
||||
newUser.google.id = profile.id;
|
||||
newUser.google.token = token;
|
||||
newUser.google.name = profile.displayName;
|
||||
newUser.google.email = profile.emails[0].value; // pull the first email
|
||||
|
||||
// save the user
|
||||
newUser.save(function(err) {
|
||||
if (err)
|
||||
throw err;
|
||||
return done(null, newUser);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}));
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user