【问题标题】:passport Js:redirect to another url with data护照Js:重定向到另一个带有数据的网址
【发布时间】:2018-06-24 13:52:51
【问题描述】:

从 passportjs 文档中,我可以重定向到 URL

app.post('/login',
  passport.authenticate('local', { successRedirect: '/',
                                   failureRedirect: '/login' }));

是否有可能successRedirect 应该是另一个不同的域假设 我的身份验证在www.auth.com 成功登录后我想使用来自www.auth.com 的数据重定向到www.customer.com/callback

这实际上就像Facebook在成功登录Facebook POST数据到回调URL后使用回调URL进行身份验证

【问题讨论】:

    标签: javascript node.js express passport.js


    【解决方案1】:
    app.get('/login', function(req, res) { //will handle if user fails to enter valid credentials
      return res.render('login', {
        message: 'Login Page Renderd...'
      });
    });
    
    app.post('/login', passport.authenticate('local', {
      successRedirect: '/profile', //will be redirected to `/profile` route after successful login
      failureRedirect: '/login' //will be redirected to `/login` route for failure
    }));
    
    app.get('/profile', function(req, res) { //will handle if user enter valid credentials means user logged in successfully
      return res.status(200).send('User Logged in successfully...');
    });
    

    Passport-Facebook 我们需要定义认证成功和失败的回调 url 与 Passport-Local 相同

    【讨论】:

    • 此解决方案仅重定向到同一域中的页面,但我想重定向到另一个域中的页面
    • 您可以在 state 参数中传递 URL,然后在回调中使用它来重定向。
    • @iambatman,您不能从服务器端代码重定向到另一个应用程序。您可以从服务器获得响应,然后检查是否正常(200)然后重定向到另一个应用程序。 (抱歉回复晚了):-)
    • @lone_worrior,是的,我们可以做到。但是在登录时传递URL中的参数是不正确的,以便创建一个好的应用程序。
    猜你喜欢
    • 2016-03-31
    • 2020-09-29
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多