Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var mongoose = require('mongoose');
- var bcrypt = require('bcrypt-nodejs');
- var Schema = mongoose.Schema;
- var userSchema = new Schema({
- local : {
- email : String,
- password: String
- },
- facebook : {
- id : String,
- token : String,
- email : String,
- name : String
- },
- twitter : {
- id : String,
- token : String,
- displayName : String,
- username : String
- },
- google : {
- id : String,
- token : String,
- email : String,
- name : String
- }
- });
- // methods ======================
- // generating a hash
- userSchema.methods.generateHash = function(password) {
- return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
- };
- // checking if password is valid
- userSchema.methods.validPassword = function(password) {
- return bcrypt.compareSync(password, this.local.password);
- };
- module.exports = mongoose.model('User', userSchema);
- passport.use('local-login', new LocalStrategy({
- usernameField : 'email',
- passwordField : 'password',
- passReqToCallback : true
- },
- function(req, email, password, done) {
- User.findOne({ 'local.email' : email }, function(err, user) {
- else
- if (err)
- return done(err);
- // if no user is found, return the message
- if (!user)
- return done(null, false, req.flash('loginMessage', 'No user found.'));
- // if the user is found but the password is wrong
- if (!user.validPassword(password))
- return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
- // all is well, return successful user
- return done(null, user);
- });
- }));
- ////////////////////////
- passport.use('local-signup', new LocalStrategy({
- usernameField : 'email',
- passwordField : 'password',
- passReqToCallback : true
- },
- function(req, email, password, done) {
- // asynchronous
- // User.findOne wont fire unless data is sent back
- process.nextTick(function() {
- User.findOne({ 'local.email' : email }, function(err, user){
- // if there are any errors, return the error
- if (err)
- return done(err);
- // check to see if theres already a user with that email
- if (user) {
- return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
- } else {
- // if there is no user with that email
- // create the user
- var newUser = new User();
- // set the user's local credentials
- newUser.local.email = email;
- newUser.local.password =password;//newUser.generateHash(password);
- // save the user
- newUser.save(function(err) {
- if (err)
- throw err;
- return done(null, newUser);
- });
- }
- });
- });
- }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement