栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我已经为我的应用程序Twitter身份验证弄清楚了这一点,我确信GoogleStrategy非常相似。尝试这样的变体:

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

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

只需将该块更改为:

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);});

现在,为经过身份验证的路由定义中间件,以将原始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('/');}

作品!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/469151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号