Created
December 5, 2015 02:47
-
-
Save mekhami/1c9b02a1243795e6c68e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var router = express.Router(); | |
| var mongoose = require('mongoose'); | |
| var User = require('../models/user.js'); | |
| var passport = require('passport'); | |
| router.post('/register', function(req, res) { | |
| User.register(new User({ username: req.body.username}), req.body.password, function(err, account) { | |
| if (err) { | |
| return res.status(500).json({err:err}); | |
| } | |
| passport.authenticate('local')(req, res, function () { | |
| return res.status(200).json({status: 'Registration successful!'}); | |
| }); | |
| }); | |
| }); | |
| router.post('/login', function(req, res, next) { | |
| passport.authenticate('local', function(err, user, info) { | |
| if (err) { | |
| return res.status(500).json({err: err}) | |
| }; | |
| if (!user) { | |
| return res.status(401).json({err: info}) | |
| }; | |
| req.logIn(user, function(err) { | |
| if (err) { | |
| return res.status(500).json({err: 'Could not log in user'}); | |
| } | |
| res.status(200).json({status: 'Login successful!'}); | |
| }); | |
| })(req, res, next); | |
| }); | |
| router.get('/logout', function(req, res) { | |
| req.logout(); | |
| res.status(200).json({status: 'Bye!'}); | |
| }); | |
| module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment