Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://localhost:8000/admin/auth/admin/home
- const express = require('express');
- const router = express.Router();
- const bcrypt = require('bcryptjs');
- const AdminUser = require('../models/adminUserModel');
- const passport = require('passport');
- router.get('/login', (req, res) => {
- res.render('login');
- });
- router.post('/login', (req, res, next) => {
- passport.authenticate('local',{
- successRedirect:'/admin/home',
- failureRedirect: '/admin/auth/register',
- failureFlash: true
- })(req, res, next);
- });
- router.get('/logout', (req, res) => {
- req.logout();
- res.render('login');
- //res.redirect('/admin/logout');
- });
- router.get('/register', (req, res) => {
- res.render('register');
- });
- router.post('/register', (req, res) => {
- req.checkBody('username', 'Enter Username').notEmpty();
- req.checkBody('email', 'Enter Valid Email').isEmail();
- req.checkBody('password', 'Enter Your Password').notEmpty();
- req.checkBody('confirmPass', 'Passwords Don't
- Match').equals(req.body.password);
- let username = req.body.username;
- let email = req.body.email;
- let password = req.body.password;
- let passwordHash = null;
- bcrypt.hash(password, 10, (err, hash) => {
- passwordHash = hash;
- console.log('Pass Hash:t' + passwordHash);
- let adminUser = new AdminUser({
- username: username,
- email: email,
- password: passwordHash
- });
- adminUser.save()
- .then(admin => {
- if (admin) {
- let admin_id = admin._id;
- console.log('Admin id:t' + admin_id);
- res.redirect('admin/home'); // problem with route I stated above
- }
- })
- .catch(errs => {
- throw errs;
- });
- });
- });
- module.exports = router;
- // routes
- app.use('/admin/auth', adminAuth); // for register and login
- app.use('/admin/home', adminIndex);
- // and passport middleware after
- const passport = require('passport');
- const passportConfig = require('./config/passport')(passport); // this is not used according to my ide
- .../// skip some code...
- app.use(passport.initialize()); // should this be passportConfig instead
- app.use(passport.session());
- app.get('*', (req, res, next) => {
- res.locals.user = req.user || null;
- next();
- });
- const LocalStrategy = require('passport-local').Strategy;
- const AdminUser = require('../models/adminUserModel');
- const bcrypt = require('bcryptjs');
- const config = require('../config/db'); // this is also unused according to my ide
- module.exports = ( (passport) => {
- passport.use(new LocalStrategy((email, password, done) => {
- AdminUser.findOne({email: email})
- .exec()
- .then(user => {
- if(!user){
- return done(null, false, {message: 'No Such User with Email Exists'});
- } else {
- bcrypt.compare(password, user.password, (err, match) =>
- {
- if(err){
- throw err;
- }
- if (match){
- return done(null, user, {message: 'User Matched'});
- } else {
- return done(null, false, {message: 'Passwords Don't Match'});
- }
- });
- passport.serializeUser((user, done) => {
- done(null, user.id);
- });
- passport.deserializeUser((id, done) => {
- AdminUser.findById({_id: id})
- .exec()
- .then(user => {
- done(null, user);
- })
- .catch(err => {
- throw err;
- })
- });
- }
- })
- .catch(errs => {
- throw errs;
- });
- }));
- });
Add Comment
Please, Sign In to add comment