【问题标题】:how to integrate passport-facebook and jwt without session?如何在没有会话的情况下集成passport-facebook和jwt?
【发布时间】:2018-03-06 04:29:44
【问题描述】:

我想通过 jwt 令牌使用 passport-github 或 facebook 登录,而不使用在服​​务器上保存会话。 但是我们有两个来自前端的请求:

app.get('/auth/facebook',
  passport.authenticate('facebook'));

app.get('/auth/facebook/callback',
  passport.authenticate('facebook', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

如何处理前端代码?

正常情况下,我们只有一个请求

axios.post(`${API_URL}/auth/login`, { email, password })
.then(response => {
  cookie.save('token', response.data.token, { path: '/' });
  dispatch({ type: AUTH_USER });
  window.location.href = CLIENT_ROOT_URL + '/dashboard';
})
.catch((error) => {
  errorHandler(dispatch, error.response, AUTH_ERROR)
});
}

所以我们可以在本地保存令牌。但是对于 passport-facebook,我们有两个请求('/auth/facebook' 和 '/auth/facebook/callback')。那么如何在本地保存token呢?

【问题讨论】:

  • 如何向客户端发送 jwt 令牌?

标签: jwt passport.js passport-facebook express-jwt


【解决方案1】:

首先,我认为 GET 请求不起作用。您需要使用链接来触发 /auth/login。

<a href="http://localhost:5150/auth/facebook">

要将令牌发送到客户端,您应该重定向到 jwt 存储在 cookie 中的客户端页面。

const token = user.generateJwt();
res.cookie("auth", token);
return res.redirect(`http://localhost:3000/socialauthredirect`);

并在客户端登录页面提取 jwt 并将其保存到本地存储。

class SocialAuthRedirect extends Component {
  componentWillMount() {
    this.props.dispatch(
      fbAuthUser(getCookie("auth"), () => {
        document.cookie =
          "auth=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
        this.props.history.push("/profile");
      })
    );
  }

  render() {
    return <div />;
  }
}

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 2016-07-26
    • 2014-08-27
    • 1970-01-01
    • 2020-09-04
    • 2011-01-31
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多