我将护照与回送集成在一起,并且工作正常,问题在于如何获取访问 token 。
我有一个网络应用程序(在与回送不同的服务器中提供):
我可以重定向(如果登录成功)到我的Web应用程序,但是我在进度中丢失了accessToken。
有任何想法吗?
我提出要求
解:
访问 token 是通过cookie中的环回设置的,因此当重定向回Web应用程序时,可以通过以下方式访问:
document.cookie
如果您想通过变量:
var access_token = document.cookie.replace(/(?:(?:^|.*;\s*)access_token\s*\=\s*([^;]*).*$)|^.*$/, "$1")
var userId = document.cookie.replace(/(?:(?:^|.*;\s*)userId\s*\=\s*([^;]*).*$)|^.*$/, "$1")
但
如果您为后端和前端使用不同的URL,则无法
所以我建议使用这篇文章的解决方案
最佳答案
我希望,我明白了。这是我遵循护照/ facebook策略的流程。
1)客户端(网络应用):window.location = http://urlServerName:port/passport/facebook
这也可以是服务器端的重定向。
2)用户在Facebook上输入凭据。
3)Facebook重定向到回调。
router.get('/passport/facebook', passport.authenticate('facebook'));
router.get('/passport/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }),
(req, res) => {
let url = req.url; // url contains the code
let urs = req.usr; // user info.
// You can set a cookie with the info you want. This can be the auth code, the user profile or a JWT generated in the same request.
res.cookie("data",usr,{httpOnly:true});
res.redirect('urlWebApplication');
});
4)在回调中,您可以设置包含所需信息的cookie。这样,就可以在您的Web应用程序中进行访问。
希望这有助于澄清。