mirror of
https://github.com/mmumshad/ansible-playable.git
synced 2025-03-09 23:38:54 +00:00
Initial Commit
This commit is contained in:
commit
c92f737237
273 changed files with 16964 additions and 0 deletions
24
server/auth/local/index.js
Normal file
24
server/auth/local/index.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
'use strict';
|
||||
|
||||
import express from 'express';
|
||||
import passport from 'passport';
|
||||
import {signToken} from '../auth.service';
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
router.post('/', function(req, res, next) {
|
||||
passport.authenticate('local', function(err, user, info) {
|
||||
var error = err || info;
|
||||
if(error) {
|
||||
return res.status(401).json(error);
|
||||
}
|
||||
if(!user) {
|
||||
return res.status(404).json({message: 'Something went wrong, please try again.'});
|
||||
}
|
||||
|
||||
var token = signToken(user._id, user.role);
|
||||
res.json({ token });
|
||||
})(req, res, next);
|
||||
});
|
||||
|
||||
export default router;
|
35
server/auth/local/passport.js
Normal file
35
server/auth/local/passport.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
import passport from 'passport';
|
||||
import {Strategy as LocalStrategy} from 'passport-local';
|
||||
|
||||
function localAuthenticate(User, email, password, done) {
|
||||
User.findOne({
|
||||
email: email.toLowerCase()
|
||||
}).exec()
|
||||
.then(user => {
|
||||
if(!user) {
|
||||
return done(null, false, {
|
||||
message: 'This email is not registered.'
|
||||
});
|
||||
}
|
||||
user.authenticate(password, function(authError, authenticated) {
|
||||
if(authError) {
|
||||
return done(authError);
|
||||
}
|
||||
if(!authenticated) {
|
||||
return done(null, false, { message: 'This password is not correct.' });
|
||||
} else {
|
||||
return done(null, user);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(err => done(err));
|
||||
}
|
||||
|
||||
export function setup(User/*, config*/) {
|
||||
passport.use(new LocalStrategy({
|
||||
usernameField: 'email',
|
||||
passwordField: 'password' // this is the virtual field on the model
|
||||
}, function(email, password, done) {
|
||||
return localAuthenticate(User, email, password, done);
|
||||
}));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue