{"size": 1072, "ext": "js", "max_stars_repo_path": "config/auth.js", "max_stars_repo_name": "ArikBartzadok/Blog-app", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "config/auth.js", "max_issues_repo_name": "ArikBartzadok/Blog-app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "config/auth.js", "max_forks_repo_name": "ArikBartzadok/Blog-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.4848484848, "max_line_length": 89, "alphanum_fraction": 0.5447761194}
const localStrategy = require('passport-local').Strategy
const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')

// Carregando models
require('../models/User')
const User = mongoose.model('user')

module.exports = function(passport){
    passport.use(new localStrategy({usernameField: 'email'}, (email, password, done) => {
        User.findOne({email: email}).then((user) => {
            if(!user){
                return done(null, false, {message: 'Esta conta não existe!'})
            }
            bcrypt.compare(password, user.password, (error, match) => {
                if(match){
                    return done(null, user)
                }else{
                    return done(null, false, {message: 'Senha incorreta'})
                }
            })
        })
    }))
    // Salvando os dados do usuário em uma sessão
    passport.serializeUser((user, done) => {
        done(null, user.id)
    })
    passport.deserializeUser((id, done) => {
        User.findById(id, (error, user) => {
            done(error, user)
        })
    })
}