2017-04-11 17:41:14 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
|
|
|
const validator = require('validator')
|
|
|
|
const LocalStrategy = require('passport-local').Strategy
|
|
|
|
const config = require('../../../config')
|
|
|
|
const models = require('../../../models')
|
|
|
|
const logger = require('../../../logger')
|
2019-05-30 18:27:56 -04:00
|
|
|
const { urlencodedParser } = require('../../utils')
|
2019-10-27 08:51:53 -04:00
|
|
|
const errors = require('../../../errors')
|
2017-04-11 17:41:14 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const emailAuth = module.exports = Router()
|
2017-04-11 17:41:14 -04:00
|
|
|
|
|
|
|
passport.use(new LocalStrategy({
|
|
|
|
usernameField: 'email'
|
|
|
|
}, function (email, password, done) {
|
|
|
|
if (!validator.isEmail(email)) return done(null, false)
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
email: email
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (!user) return done(null, false)
|
2019-05-13 05:51:05 -04:00
|
|
|
user.verifyPassword(password).then(verified => {
|
|
|
|
if (verified) {
|
|
|
|
return done(null, user)
|
|
|
|
} else {
|
|
|
|
logger.warn('invalid password given for %s', user.email)
|
|
|
|
return done(null, false)
|
|
|
|
}
|
|
|
|
})
|
2017-04-11 17:41:14 -04:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return done(err)
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.allowEmailRegister) {
|
2017-04-11 17:41:14 -04:00
|
|
|
emailAuth.post('/register', urlencodedParser, function (req, res, next) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (!req.body.email || !req.body.password) return errors.errorBadRequest(res)
|
|
|
|
if (!validator.isEmail(req.body.email)) return errors.errorBadRequest(res)
|
2017-04-11 17:41:14 -04:00
|
|
|
models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
email: req.body.email
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
password: req.body.password
|
|
|
|
}
|
|
|
|
}).spread(function (user, created) {
|
|
|
|
if (user) {
|
|
|
|
if (created) {
|
|
|
|
logger.debug('user registered: ' + user.id)
|
|
|
|
req.flash('info', "You've successfully registered, please signin.")
|
|
|
|
} else {
|
|
|
|
logger.debug('user found: ' + user.id)
|
|
|
|
req.flash('error', 'This email has been used, please try another one.')
|
|
|
|
}
|
2018-03-07 09:17:35 -05:00
|
|
|
return res.redirect(config.serverURL + '/')
|
2017-04-11 17:41:14 -04:00
|
|
|
}
|
|
|
|
req.flash('error', 'Failed to register your account, please try again.')
|
2018-03-07 09:17:35 -05:00
|
|
|
return res.redirect(config.serverURL + '/')
|
2017-04-11 17:41:14 -04:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('auth callback failed: ' + err)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
2017-04-11 17:41:14 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
emailAuth.post('/login', urlencodedParser, function (req, res, next) {
|
2019-10-27 08:51:53 -04:00
|
|
|
if (!req.body.email || !req.body.password) return errors.errorBadRequest(res)
|
|
|
|
if (!validator.isEmail(req.body.email)) return errors.errorBadRequest(res)
|
2017-04-11 17:41:14 -04:00
|
|
|
passport.authenticate('local', {
|
2018-03-07 09:17:35 -05:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/',
|
2017-04-11 17:41:14 -04:00
|
|
|
failureFlash: 'Invalid email or password.'
|
|
|
|
})(req, res, next)
|
|
|
|
})
|