Hi,
The method isAuthenticated() is not mentioned anywhere in the docs.Also is there any method named isUnauthenticated() too?
Also is there any method named isUnauthenticated() too?
Yes this is a big omission since it's kind of the point of the library :) Here's a page documenting it: https://stackoverflow.com/questions/17756848/only-allow-passportjs-authenticated-users-to-visit-protected-page
Here's example code. Feedback on better approaches would be appreciated:
const express = require('express');
const app = express();
// must be the first defined route
app.all('*', (request, response, next) => {
let publicRoutes = ['/register', '/login', '/logout'];
(publicRoutes.includes(request.path) || request.isAuthenticated()) ? next() : response.sendStatus(401);
});
app.get('/myprotectedroute', (request, response) => response.send('Hello!'));
app.listen(3000);
So what's the bug here?
There is no bug... The thing is that the isAuthenticated() and isUnauthenticated() functions are not mentioned anywhere in the docs. A lot of people keep requesting that this should be in the documentation since the whole point of using passport is for authenticating the user... I really don't know why this was not added yet, I could find an issue in GitHub from 2016 asking for this...
Just started using Passport and really struggled with this. Some mention in the docs would be good!
Can't believe this issue is not fixed yet, it's been 2.5 years but they haven't updated the docs to solve the problem.
also the examples page for passport-local doesn't exist anymore.
Most helpful comment
There is no bug... The thing is that the isAuthenticated() and isUnauthenticated() functions are not mentioned anywhere in the docs. A lot of people keep requesting that this should be in the documentation since the whole point of using passport is for authenticating the user... I really don't know why this was not added yet, I could find an issue in GitHub from 2016 asking for this...