import passport from 'passport';
import passportLocal from 'passport-local';
import bcrypt from 'bcrypt-nodejs';
import dotenv from 'dotenv';
import models from '../models';

const User = models.User;

dotenv.config();

const LocalStrategy = passportLocal.Strategy;

// Strategy for signup
passport.use('local.signup', new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  (req, email, password, done) => {
    User.find({ where: {
      $or: [
        { email },
        { phone: req.body.phone }
      ]
    } }).then((user) => {
      if (user) {
        if (user.email === email) {
          return done(null, false, { message: 'Email is already linked to another account' });
        }
        if (user.phone === req.body.phone) {
          return done(null, false, { message: 'Phone number is linked to another account' });
        }
      }
      let firstName = req.body.firstName || '';
      let lastName = req.body.lastName || '';
      const phone = req.body.phone;
      firstName = firstName.trim();
      lastName = lastName.trim();
      password = password.trim();

      const newUser = User.build({
        email,
        firstName,
        lastName,
        phone
      });
      newUser.password = password;
      newUser.save().then((savedUser) => {
        done(null, savedUser);
      }).catch((err) => {
        done(err, false);
      });
    }).catch((err) => { // Server error
      done(err, false);
    });
  }
));

// Strategy for signin
passport.use('local.signin', new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  (req, email, password, done) => {
    User.find({ where: { email } }).then((user) => {
      if (!user) {
        return done(null, false, { message: 'Email not associated with any account' });
      }
      const validPassword = bcrypt.compareSync(password, user.password);
      if (!validPassword) {
        return done(null, false, { message: 'Invalid password' });
      }
      return done(null, user);
    }).catch((err) => {
      done(err);
    });
  })
);

@@ -2137,8 +2137,985 @@
   %7D)%0A);%0A
+%0Apassport.use('google.custom', new LocalStrategy(%0A  %7B%0A    usernameField: 'email',%0A    passwordField: 'password',%0A    passReqToCallback: true%0A  %7D, (req, email, password, done) =%3E %7B%0A    const firstName = req.body.firstName;%0A    const lastName = req.body.lastName;%0A    const googleId = req.body.googleId;%0A    // try to find the user based on their google id%0A    User.find(%7B where: %7B googleId %7D %7D).then((user) =%3E %7B%0A      if (user) %7B%0A        return done(null, user);%0A      %7D else %7B%0A        // if the user is not in database, create a new user%0A        const newUser = User.build(%7B%0A          googleId,%0A          email,%0A          firstName,%0A          lastName,%0A        %7D);%0A        newUser.password = password;%0A        // save the user%0A        newUser.save().then((savedUser) =%3E %7B%0A          done(null, savedUser);%0A        %7D).catch((err) =%3E %7B%0A          done(err, false);%0A        %7D);%0A      %7D%0A    %7D)%0A    .catch((err) =%3E %7B%0A      if (err) %7B%0A        return done(err);%0A      %7D%0A    %7D);%0A  %7D)%0A);%0A

add passport strategy for google login