我想使用passport-github或带有jwt令牌的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')。那么如何在本地保存令牌呢?
发布于 2018-03-01 10:36:56
首先,我认为GET请求不会起作用。您需要使用a链接来触发/auth/login。
<a href="http://localhost:5150/auth/facebook">要将令牌发送到客户端,您应该重定向到包含存储在cookie中的jwt的客户端页面。
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 />;
}
}https://stackoverflow.com/questions/46401211
复制相似问题