Express req.isAuthenticated()始终为false
我使用passportjs的身份验证功能将始终返回false,即使用户已经存在,并且它将始终重定向到登录页面,这将覆盖我的所有身份验证路由,因此当我使用有效的用户凭据登录或创建新用户时,默认行为是重定向到“机密页面”,但每次只重定向到登录页面 我不知道我做错了什么,伙计们,我需要你们的帮助,请。。。 我已经看到了其他相关的问题,但大多数线程并没有真正回答这些问题,或者看起来像解决方案的答案即使我应用了它也不起作用,因为它们应该是我仍然不知道该怎么做才能使它起作用 我已经编写了一个简单的应用程序,使用routes和passportjs验证用户登录注册和注销 我的最后一段代码设置为仅允许用户访问主站点的内容,在本例中,仅当用户是有效用户(即他们已登录或已成功注册)时,该主站点称为秘密模板 我创建的函数如下所示:Express req.isAuthenticated()始终为false,express,authentication,mongoose,passport.js,passport-local,Express,Authentication,Mongoose,Passport.js,Passport Local,我使用passportjs的身份验证功能将始终返回false,即使用户已经存在,并且它将始终重定向到登录页面,这将覆盖我的所有身份验证路由,因此当我使用有效的用户凭据登录或创建新用户时,默认行为是重定向到“机密页面”,但每次只重定向到登录页面 我不知道我做错了什么,伙计们,我需要你们的帮助,请。。。 我已经看到了其他相关的问题,但大多数线程并没有真正回答这些问题,或者看起来像解决方案的答案即使我应用了它也不起作用,因为它们应该是我仍然不知道该怎么做才能使它起作用 我已经编写了一个简单的应用程序,
// Authenticate user Login
function isLoggedIn(req, res, next) {
if(req.isAuthenticated()) {
return next();
}
res.redirect('/login');
}
这基本上是为了检查用户是否已经登录
然后,我在我的一条路径中将该函数作为中间件调用:
app.get('/secret', isLoggedIn , (req, res)=>{
res.render('secret');
});
这是为了确保用户在访问机密页面之前已登录或已注册,否则,应返回登录页面并要求用户登录或已注册以访问机密页面
这是我的全部代码,以防万一,你的眼睛比我的更敏锐
var express = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require ('body-parser'),
User = require('./models/user'),
passport = require('passport'),
localStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose');
mongoose.connect('mongodb://localhost/auth_demo_app', {
useNewUrlParser: true
});
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(require("express-session")({
secret: "Rusty is the worst and ugliest dog in the wolrd",
resave: true,
saveUninitialized: true
}));
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// ==================================================
// ROUTES
// ==================================================
app.get('/', (req, res)=>{
res.render('home');
});
app.get('/secret',isLoggedIn, (req, res)=>{
res.render('secret');
});
// AUTH ROUTES
// Register - Show Registration form
app.get('/register', (req, res)=>{
res.render('register');
});
// Handle user Signup
app.post('/register', (req, res)=>{
req.body.username
req.body.password
User.register(new User({username: req.body.username}), req.body.password, (err, user)=>{
if(err){
console.log(err);
return res.render('register');
}
passport.authenticate('local')(req, res, ()=>{
res.redirect('/secret');
})
})
});
// Login - Show Login form
app.get('/login', (req, res)=>{
res.render('login');
});
// Handle user Signup
app.post('/login', passport.authenticate('local', {
successRedirect: '/secret',
failureRedirect: '/login',
}),(req, res)=>{
// Other stuff goes here
});
// LOGOUT ROUTE
// Logs user out - ends user session
app.get('/logout', (req, res)=>{
req.logOut();
res.redirect('/');
});
// Authenticate user Login
function isLoggedIn(req, res, next) {
if(req.isAuthenticated()) {
console.log('User logged in successfully');
return next();
}
res.redirect('/login');
}
app.listen(3000, ()=>{
console.log('Server Started...');
});
console.log(req.isAuthenticated())//总是返回false。尝试更改
app.use(passport.initialize());
app.use(passport.session());
app.use(require("express-session")({
secret: "Rusty is the worst and ugliest dog in the wolrd",
resave: true,
saveUninitialized: true
}));
到
如果您正在使用cookie,请确保添加cookie解析器中间件
var express = require('express')
var cookieParser = require('cookie-parser')
var app = express()
app.use(cookieParser())
如果情况并非如此,请检查呼叫端,如果您使用的是axios includewith Credentials
axios.get('some api url', {withCredentials: true});
如果您是uisg获取请确保添加凭据:“包括”
fetch('/...', {
method: ..,
headers: ...,
credentials: 'include',
body: ...
...})
谢谢,我发现这是我很欣赏的问题。
fetch('/...', {
method: ..,
headers: ...,
credentials: 'include',
body: ...
...})