Manual passport for authentication

I am creating a soothing nodeJS API protected by oauth2 authentication using passport.

var express = require('express'); var passport = require('passport'); var port = process.env.PORT || 8080; var app = express(); app.use(passport.initialize()); // Create our Express router var router = express.Router(); var creatureController = require('./controllers/creature'); router.route('/creature').get(passport.authenticate('accessToken', {session: false}), creatureController.getProfile); 

In this case, the route is protected and it needs to send a valid token to access the route.

I want to find a way to authenticate my "users" manually by calling a function that accepts the username and password of the user I want to verify.

+6
source share
1 answer

The passport provides the req.login() function, which can be used to manually enter the user.

 app.post('/login', function (req, res, next) { var user = User.findOrCreate(req.body); // … your authentication or whatever req.login(user, function(err){ if(err) return next(err); res.redirect('/home'); }); }); 
+8
source

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


All Articles