There is a code, all that it does is check the validity of the username and password

If both criteria are entered correctly, we are transferred to admin.html, if not, we remain at login.html, this code (Strategy) is only for the admin page of the project, I also want to make a strategy with cookies on users, how to do it correctly? I mean how to connect the second strategy so that they (and the first and second) do not conflict with each other !? And in the second strategy I want to check for login and password.

Thanks in advance for your help!

var express = require("express"); var app = express(); var bodyParser = require("body-parser"); var Admin = require('./models/admin'); var cookieParser = require('cookie-parser'); app.use(cookieParser()); var session = require('cookie-session'); app.use(session({keys:['secret'],maxAge: 2*60*60*1000})); var passport = require('passport'); app.use(passport.initialize()); app.use(passport.session()); var localStrategy = require('passport-local').Strategy passport.use(new localStrategy(function(username,password,done){ Admin.find({username: username, password: password},function(err,data){ if(data.length == 1){ return done(null,{id: data[0]._id}); } else{ return done(null,false); } }); })); passport.serializeUser(function(user,done){ done(null,user.id); }); passport.deserializeUser(function(id,done){ Admin.find({_id: id},function(err,data){ done(null,{username:data[0].username, id: data[0]._id}); }); }); var auth = passport.authenticate('local',{ successRedirect: '/admin', failureRedirect: '/login' }); var myAuth = function(req,res,next){ if(req.isAuthenticated()) next(); else res.redirect('/login'); } app.post('/login',auth); app.get('/admin',myAuth); app.get('/login',function(req,res){ res.sendFile(__dirname + '/viewAdmin/login.html'); }); app.get('/admin',function(req,res){ res.sendFile(__dirname + '/viewAdmin/admin.html'); }); 
  • You have a myAuth function that checks whether a user is avorized. Write the adminOnly function, which will check if the user is an administrator, then give him a page, and if not, transfer to the main (or any other) page. - Bim Bam
  • Thank you, even though I figured it out (by adding a new model through facebook strategy), but I'll try your version on a new project. -

0