2020-11-20 10:02:30 +00:00
|
|
|
const passport = require('passport');
|
|
|
|
const LocalStrategy = require('passport-local').Strategy;
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const User = mongoose.model('User');
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-20 10:02:30 +00:00
|
|
|
passport.use(
|
|
|
|
new LocalStrategy(
|
|
|
|
{
|
|
|
|
usernameField: 'user[email]',
|
|
|
|
passwordField: 'user[password]',
|
|
|
|
},
|
2020-11-20 10:30:12 +00:00
|
|
|
async function (email, password, done) {
|
|
|
|
try {
|
|
|
|
const user = await User.findOne({ email: email });
|
|
|
|
if (!user || !user.validPassword(password)) {
|
|
|
|
return done(null, false, { errors: { 'email or password': 'is invalid' } });
|
|
|
|
}
|
2020-10-20 19:25:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
if (user.needsEmailValidation) {
|
|
|
|
return done(null, false, { errors: { 'E-Mail-Bestätigung': 'noch nicht erfolgt' } });
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
return done(null, user);
|
|
|
|
} catch (err) {
|
|
|
|
done(err);
|
|
|
|
}
|
2020-11-20 10:02:30 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|