且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用passport-google-oauth成功身份验证后重定向到原始页面

更新时间:2023-09-20 22:03:34

还可以存储会话中的第一个 URL,前提是当前未设置该变量.如果设置了此变量,则发布登录信息,然后重新指向该变量.

Can also store the first URL in the session, only if the variable is currently not set. Then post login if this var is set, then re direct to that.

这比来自 3rd 方 oauth 的标头可能不准确.

This is better than a header which might not be accurate from a 3rd party oauth.

在 Java 中,我会在过滤器中执行此操作(检查会话变量,例如 redirect_to 或 first_url,如果它为 null,则设置它,如果不是登录/注册页面).在节点 js 中可以使用中间件,如 https://***.com/a/13336055/1643558

In Java I would do it in a filter (check for a session variable to, say redirect_to or first_url, if its null then set it, if not the login/ sign up page). In node js can use middleware as explained in https://***.com/a/13336055/1643558

引用那个答案:我有一个与 app.get('/account', auth.restrict, routes.account) 一起使用的中间件,它在会话中设置了 redirect_to...然后我重定向到/login

Quoting that answer: I have a middleware I use with app.get('/account', auth.restrict, routes.account) that sets redirect_to in the session...then I redirect to /login

auth.restrict = function(req, res, next){
    if ( !req.session.userid ) {
        req.session.redirect_to = '/account';
            res.redirect('/login');
    } else {
        next();
    }
};

然后在 routes.login.post 中,我执行以下操作:

Then in routes.login.post I do the following:

var redirect_to = req.session.redirect_to ? req.session.redirect_to : '/';
delete req.session.redirect_to;
//is authenticated ?
res.redirect(redirect_to);