Skip to content

Instantly share code, notes, and snippets.

@jaouadballat
Last active May 4, 2018 21:40
Show Gist options
  • Save jaouadballat/521d86faf25fd54bc4557e24c2865854 to your computer and use it in GitHub Desktop.
Save jaouadballat/521d86faf25fd54bc4557e24c2865854 to your computer and use it in GitHub Desktop.
var express = require('express');
var router = express.Router();
const User = require('../models/user');
/* GET users listing. */
router.post('/', function(req, res, next) {
const user = new User({
email: req.body.email,
password: req.body.password
});
user.save(function(err, user) {
if(err) res.send(err)
res.status(200).send(user);
});
});
router.post('/login', function(req, res, next) {
let email = req.body.email;
let password = req.body.password;
User.findOne({email: email}, function(err, user) {
if(err) throw err;
if(!user) {
res.status(404).send('User not found')
}else {
user.comparePassword(password, function(err, isMatch) {
if(!isMatch) {
res.send('Password Incorrect')
}else {
res.send('Login')
}
});
}
});
});
router.get('/users', function(req,res, next) {
User.find().select('-password').exec(function(err, users) {
res.json(users)
});
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment