在Node.js Passport的Google策略上自定义returnUrl

bub*_*son 19 node.js express passport.js

我正在使用Express和Passport OpenID Google策略,我想在每个auth请求上设置returnURL,以便能够返回启动该身份验证的页面.

情况是我有带有Node.js后端的HTML5幻灯片应用程序(以及社交内容和编辑器以及Portal和扩展... https://github.com/bubersson/humla)我希望能够登录用户一些幻灯片(通过幻灯片菜单...)但我希望他能轻松回到同一张幻灯片.

所以我需要这样的东西?

app.get('/auth/google', function(req,res) {
   var cust = "http://localhost:1338/"+req.params.xxx;
   passport.authenticate('google', returnURL:cust, function ...
} 
Run Code Online (Sandbox Code Playgroud)

我读过Passport的指南,但仍然不知道该怎么做.我知道这不安全,但我怎么办呢?

或者,如何让应用程序返回到启动登录的页面?或者有没有办法使用AJAX进行OpenID身份验证(并且仍然可以使用护照)?

mko*_*nen 24

我已经想到了我的应用程序Twitter身份验证,我相信GoogleStrategy非常相似.试试这个的变种:

假设您已经从身份验证服务中定义了回调路由(如护照指南):

app.get('/auth/twitter/callback',
  passport.authenticate('twitter', {
      successRedirect: authenticationRedirect(req, '/account')
    , failureRedirect: '/'
  })
);
Run Code Online (Sandbox Code Playgroud)

只需将该块更改为:

app.get('/auth/twitter/callback', function(req, res, next){
  passport.authenticate('twitter', function(err, user, info){
    // This is the default destination upon successful login.
    var redirectUrl = '/account';

    if (err) { return next(err); }
    if (!user) { return res.redirect('/'); }

    // If we have previously stored a redirectUrl, use that, 
    // otherwise, use the default.
    if (req.session.redirectUrl) {
      redirectUrl = req.session.redirectUrl;
      req.session.redirectUrl = null;
    }
    req.logIn(user, function(err){
      if (err) { return next(err); }
    });
    res.redirect(redirectUrl);
  })(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)

现在,为经过身份验证的路由定义中间件,以便在会话中存储原始URL,如下所示:

ensureAuthenticated = function (req, res, next) {
  if (req.isAuthenticated()) { return next(); }

  // If the user is not authenticated, then we will start the authentication
  // process.  Before we do, let's store this originally requested URL in the
  // session so we know where to return the user later.

  req.session.redirectUrl = req.url;

  // Resume normal authentication...

  logger.info('User is not authenticated.');
  req.flash("warn", "You must be logged-in to do that.");
  res.redirect('/');
}
Run Code Online (Sandbox Code Playgroud)

作品!


nam*_*uol 14

只要您有登录按钮,请将请求的当前URL附加为查询参数(根据您使用的任何模板系统进行调整):

<a href='/auth/google?redirect=<%= req.url %>'>Log In</a>
Run Code Online (Sandbox Code Playgroud)

然后,将中间件添加到GET /auth/google存储此值的处理程序中 req.session:

app.get('/auth/google', function(req, res, next) {
  req.session.redirect = req.query.redirect;
  next();
}, passport.authenticate('google'));
Run Code Online (Sandbox Code Playgroud)

最后,在您的回调处理程序中,重定向到会话中存储的URL:

app.get('/auth/google/callback', passport.authenticate('google',
  failureRedirect: '/'
), function (req, res) {
  res.redirect(req.session.redirect || '/');
  delete req.session.redirect;
});
Run Code Online (Sandbox Code Playgroud)


nak*_*nak 5

尝试res.redirect('back');回调passport.authenticate

  • 好吧,我发现了这个问题.它只有在我已经使用Google登录时才有效.如果护照重定向到Google页面然后返回我的网站 - "页面"路线将丢失.是否还有其他方法可以强制它从Google返回我的位置(启动登录的页面)? (2认同)