2017-04-11 21:41:14 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const passport = require('passport')
|
|
|
|
const validator = require('validator')
|
|
|
|
const LocalStrategy = require('passport-local').Strategy
|
2020-01-04 22:30:23 +00:00
|
|
|
const config = require('../../config')
|
|
|
|
const models = require('../../models')
|
|
|
|
const logger = require('../../logger')
|
2019-04-12 09:58:06 +00:00
|
|
|
const { setReturnToFromReferer } = require('../utils')
|
|
|
|
const { urlencodedParser } = require('../../utils')
|
2020-01-04 22:30:23 +00:00
|
|
|
const response = require('../../response')
|
2017-04-11 21:41:14 +00:00
|
|
|
|
2019-08-01 16:58:29 +00:00
|
|
|
const emailAuth = module.exports = Router()
|
2017-04-11 21:41:14 +00:00
|
|
|
|
|
|
|
passport.use(new LocalStrategy({
|
|
|
|
usernameField: 'email'
|
2020-04-08 18:42:48 +00:00
|
|
|
}, async function (email, password, done) {
|
2017-04-11 21:41:14 +00:00
|
|
|
if (!validator.isEmail(email)) return done(null, false)
|
2020-04-08 18:42:48 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const user = await models.User.findOne({
|
|
|
|
where: {
|
|
|
|
email: email
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-04-11 21:41:14 +00:00
|
|
|
if (!user) return done(null, false)
|
2020-04-08 18:42:48 +00:00
|
|
|
if (!await user.verifyPassword(password)) return done(null, false)
|
2017-04-11 21:41:14 +00:00
|
|
|
return done(null, user)
|
2020-04-08 18:42:48 +00:00
|
|
|
} catch (err) {
|
2017-04-11 21:41:14 +00:00
|
|
|
logger.error(err)
|
|
|
|
return done(err)
|
2020-04-08 18:42:48 +00:00
|
|
|
}
|
2017-04-11 21:41:14 +00:00
|
|
|
}))
|
|
|
|
|
2018-03-07 14:17:35 +00:00
|
|
|
if (config.allowEmailRegister) {
|
2020-04-08 18:42:48 +00:00
|
|
|
emailAuth.post('/register', urlencodedParser, async function (req, res, next) {
|
2020-02-26 03:22:59 +00:00
|
|
|
if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res)
|
|
|
|
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res)
|
2020-04-08 18:42:48 +00:00
|
|
|
try {
|
|
|
|
const [user, created] = await models.User.findOrCreate({
|
|
|
|
where: {
|
|
|
|
email: req.body.email
|
|
|
|
},
|
|
|
|
defaults: {
|
2020-04-14 15:17:16 +00:00
|
|
|
password: req.body.password
|
2017-04-11 21:41:14 +00:00
|
|
|
}
|
2020-04-08 18:42:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
req.flash('error', 'Failed to register your account, please try again.')
|
2018-03-07 14:17:35 +00:00
|
|
|
return res.redirect(config.serverURL + '/')
|
2017-04-11 21:41:14 +00:00
|
|
|
}
|
2020-04-08 18:42:48 +00:00
|
|
|
|
|
|
|
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 14:17:35 +00:00
|
|
|
return res.redirect(config.serverURL + '/')
|
2020-04-08 18:42:48 +00:00
|
|
|
} catch (err) {
|
2017-04-11 21:41:14 +00:00
|
|
|
logger.error('auth callback failed: ' + err)
|
2020-02-26 03:26:01 +00:00
|
|
|
return response.errorInternalError(req, res)
|
2020-04-08 18:42:48 +00:00
|
|
|
}
|
2017-04-11 21:41:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
emailAuth.post('/login', urlencodedParser, function (req, res, next) {
|
2020-02-26 03:22:59 +00:00
|
|
|
if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res)
|
|
|
|
if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res)
|
2017-04-11 21:41:14 +00:00
|
|
|
setReturnToFromReferer(req)
|
|
|
|
passport.authenticate('local', {
|
2018-03-07 14:17:35 +00:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/',
|
2017-04-11 21:41:14 +00:00
|
|
|
failureFlash: 'Invalid email or password.'
|
|
|
|
})(req, res, next)
|
|
|
|
})
|