Passport.js Session Confusion

I am learning node and expressing. I am trying to create a very simple application that will simply allow the user to log in using json. Then it will conduct a session until logging out. Using asp.net, this is the doddle that you just installed in the config and call ...

Auth.Login(username,pasword) 

When they log out, you simply do:

 Auth.logout() 

And if you need to check if they are logged in, just do:

 Auth.IsLoggedIn() 

Or the code for this. Passport for node seems just not that simple. I spent all night doing this work ...

 app.post('/authentication/login', function handleLocalAuthentication(req, res, next) { passport.authenticate('local', function(err, user, info) { // Manually establish the session... req.login({username:' me@me.com ',password:'password'}, function(err) { if (err) return next(err); return res.json({ message: 'user authenticated' }); }); })(req, res, next); }); app.get('/authentication/isauthenticated',function(req,res){ console.log(req.isAuthenticated()); }) passport.use(new LocalStrategy( function(username, password, done) { return done(null, {username:'ss',password:'sffds'}); } )); 

So now I don’t have cookies, the session is not saved when I log in, and then click URL /authentication/isAuthenticated . I can't even get a breakpoint to stay in strategy ...

 passport.use(new LocalStrategy( function(username, password, done) { console.log('ggg'); return done(null, {username:'ss',password:'sffds'}); } )); 

Am I looking at the wrong solution for this? Should I run my own auth or something with a basic middleware function?

+6
source share
1 answer

Check out this tutorial . It is really great and it helped me a lot.

And here is my repo that implemented passport authentication with users stored in mongodb through mongoose, and hashed passwords. Cloning it or just checking it should help. https://github.com/thyforhtian/auth_base .

+3
source

Source: https://habr.com/ru/post/971262/


All Articles