gpt4 book ai didi

javascript - 如何在 express + passport js 中删除注销时的 cookie?

转载 作者:行者123 更新时间:2023-11-30 12:13:27 25 4
gpt4 key购买 nike

我想“在注销时删除 cookie”。我做不到。我用谷歌搜索了答案并找到了以下方法:

  1. 为 cookie 指定新的到期日期

    res.cookie('connect.sid', '', {expires: new Date(1), path: '/' });

  2. 使用以下行删除 cookie

    res.clearCookie('connect.sid', { path: '/' });

我分别尝试了两种方法,但它们都没有删除 cookie。

这是我的代码:

routes.js

module.exports = function(app, passport, session){
app.get('/', function(req, res)
{
res.render('index.ejs');
});

app.get('/login', function(req,res){
res.render('login.ejs',{message:req.flash('loginMessage')});
});


app.get('/signup',checkRedirect , function(req, res) {
res.render('signup.ejs',{message: req.flash('signupMessage')});
});
app.get('/profile', isLoggedIn, function(req,res) {
res.render('profile.ejs', {
user :req.user
});
});
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile',
failureRedirect : '/signup',
failureFlash : true
}));
app.post('/login', passport.authenticate('local-login', {

successRedirect : '/profile',
failureRedirect : '/login',
failureFlash :true

}));
app.get('/logout',function(req,res){
res.cookie('connect.sid', '', {expires: new Date(1), path: '/' });
req.logOut();
res.clearCookie('connect.sid', { path: '/' });
res.redirect('/');
});

function isLoggedIn(req, res, next){

if(req.isAuthenticated())
return next();

console.log("hiii");
res.redirect('/');
}

};

server.js

    var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
var mongoose = require('mongoose');
var passport = require('passport');
var flash=require('connect-flash');
var morgan=require('morgan');
var bodyParser = require('body-parser');
var cookieParser=require('cookie-parser');
//
var session=require('express-session');
var RedisStore = require('connect-redis')(session);
var redis = require("redis");
var redis_client = redis.createClient();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
var configDb=require('./config/database.js');
mongoose.connect(configDb.url);

require('./config/passport')(passport);

app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser());
app.set('view engine', 'ejs');


app.use(session({
store: new RedisStore({
host: '127.0.0.1',
port: 6379,
client: redis_client
}),
secret : 'foo',
resave: false,
saveUninitialized: false
}));
app.use(function (req, res, next) {
if (!req.session) {
return next(new Error('oh no')); // handle error
}
next();
});


});

app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

require('./app/routes')(app, passport, session);
app.listen(port, function(){
console.log('server is at port' + port);
});

最佳答案

请试试这个:

router.get('/logout', function (req, res) {
req.logOut();
res.status(200).clearCookie('connect.sid', {
path: '/'
});
req.session.destroy(function (err) {
res.redirect('/');
});
});

关于javascript - 如何在 express + passport js 中删除注销时的 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33112299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com