Guest User

Untitled

a guest
Mar 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. http://localhost:8000/admin/auth/admin/home
  2.  
  3. const express = require('express');
  4. const router = express.Router();
  5. const bcrypt = require('bcryptjs');
  6. const AdminUser = require('../models/adminUserModel');
  7. const passport = require('passport');
  8.  
  9. router.get('/login', (req, res) => {
  10. res.render('login');
  11. });
  12.  
  13. router.post('/login', (req, res, next) => {
  14.  
  15. passport.authenticate('local',{
  16. successRedirect:'/admin/home',
  17. failureRedirect: '/admin/auth/register',
  18. failureFlash: true
  19. })(req, res, next);
  20.  
  21. });
  22.  
  23. router.get('/logout', (req, res) => {
  24. req.logout();
  25. res.render('login');
  26. //res.redirect('/admin/logout');
  27. });
  28.  
  29. router.get('/register', (req, res) => {
  30. res.render('register');
  31. });
  32.  
  33. router.post('/register', (req, res) => {
  34.  
  35. req.checkBody('username', 'Enter Username').notEmpty();
  36. req.checkBody('email', 'Enter Valid Email').isEmail();
  37. req.checkBody('password', 'Enter Your Password').notEmpty();
  38. req.checkBody('confirmPass', 'Passwords Don't
  39. Match').equals(req.body.password);
  40.  
  41. let username = req.body.username;
  42. let email = req.body.email;
  43. let password = req.body.password;
  44.  
  45. let passwordHash = null;
  46. bcrypt.hash(password, 10, (err, hash) => {
  47. passwordHash = hash;
  48. console.log('Pass Hash:t' + passwordHash);
  49.  
  50. let adminUser = new AdminUser({
  51. username: username,
  52. email: email,
  53. password: passwordHash
  54. });
  55.  
  56. adminUser.save()
  57. .then(admin => {
  58. if (admin) {
  59. let admin_id = admin._id;
  60. console.log('Admin id:t' + admin_id);
  61.  
  62. res.redirect('admin/home'); // problem with route I stated above
  63.  
  64. }
  65. })
  66. .catch(errs => {
  67. throw errs;
  68. });
  69.  
  70. });
  71.  
  72. });
  73.  
  74. module.exports = router;
  75.  
  76. // routes
  77. app.use('/admin/auth', adminAuth); // for register and login
  78. app.use('/admin/home', adminIndex);
  79.  
  80. // and passport middleware after
  81. const passport = require('passport');
  82. const passportConfig = require('./config/passport')(passport); // this is not used according to my ide
  83.  
  84.  
  85. .../// skip some code...
  86.  
  87.  
  88. app.use(passport.initialize()); // should this be passportConfig instead
  89. app.use(passport.session());
  90. app.get('*', (req, res, next) => {
  91. res.locals.user = req.user || null;
  92. next();
  93. });
  94.  
  95. const LocalStrategy = require('passport-local').Strategy;
  96. const AdminUser = require('../models/adminUserModel');
  97. const bcrypt = require('bcryptjs');
  98. const config = require('../config/db'); // this is also unused according to my ide
  99.  
  100. module.exports = ( (passport) => {
  101. passport.use(new LocalStrategy((email, password, done) => {
  102. AdminUser.findOne({email: email})
  103. .exec()
  104. .then(user => {
  105. if(!user){
  106. return done(null, false, {message: 'No Such User with Email Exists'});
  107. } else {
  108. bcrypt.compare(password, user.password, (err, match) =>
  109. {
  110. if(err){
  111. throw err;
  112. }
  113.  
  114. if (match){
  115. return done(null, user, {message: 'User Matched'});
  116. } else {
  117. return done(null, false, {message: 'Passwords Don't Match'});
  118. }
  119. });
  120.  
  121. passport.serializeUser((user, done) => {
  122. done(null, user.id);
  123. });
  124.  
  125. passport.deserializeUser((id, done) => {
  126. AdminUser.findById({_id: id})
  127. .exec()
  128. .then(user => {
  129. done(null, user);
  130. })
  131. .catch(err => {
  132. throw err;
  133. })
  134. });
  135.  
  136. }
  137. })
  138. .catch(errs => {
  139. throw errs;
  140. });
  141. }));
  142. });
Add Comment
Please, Sign In to add comment