node.js - 带有蒸汽护照的Angular 6 - Auth Guard问题
问题描述
我正在使用蒸汽护照登录。所以基本上,我去localhost/auth/steam
,这把我重定向到蒸汽服务,然后回到localhost/auth/steam/return
,从那里,我得到了我req.user
的预期:
router.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: 'http://localhost:4200/' }),
function(req, res) {
res.redirect('http://localhost:4200/'); --angular port application
console.log(req.user) -- I have all my datas
});
同样,当我打开浏览器并转到 时localhost/api/isLoggedIn
,我得到了我req.user
的预期:
router.get('/api/isLoggedIn', (req, res) => {
if (req.isAuthenticated()) {
console.log(req.user)
if (req.user.db) {
res.json({user: req.user.db, success: true})
return
}
}
res.json({success: false})
})
问题是当我将它与 Angular 一起使用时。window.location.href = "http://localhost:8080/auth/steam";
当我点击登录按钮时,我不得不这样做。再一次,当蒸汽服务回复我时,我有我的数据。
但是当我尝试从 Angular 访问它时:
isAuthenticated(): boolean {
let obs = this.http.get('http://localhost:8080/api/isLoggedIn').subscribe(data => {
return JSON.parse(data._body).success -- this is always FALSE!
})
}
如何管理问题?从浏览器来看,没关系,但从 Angular 来看,看起来不像是那个点击按钮的人啊。
感谢您将来的任何回答。
编辑:我进行了一些调查,当我浏览浏览器和从 Angular 发出 http 获取请求时,我没有相同的会话 /api/isLoggedIn
,如何链接两者?
浏览器的会话 ID:EvbuWUZ7vceCxHsm7npb6572bLe-1lRC
Angular的会话ID:每次我提出请求时它都会改变..
解决方案
您的isAuthenticated()
函数始终返回undefined
(可以视为错误)。
看看你的功能。你的函数没有返回任何东西。return 语句在 async 函数内部。
尝试这个。
isAuthenticated(): Observable<boolean> {
return this.http.get('http://localhost:8080/api/isLoggedIn')
.pipe(
map(data => !!JSON.parse(data._body).success)
)
}
推荐阅读
- python - Kivy 动态地将按钮添加到 gridlayout 会出错
- numpy - scipy稀疏矩阵和numpy数组之间的点积给出ValueError
- python - 如何计算输出文件中的字母 N,它是写入 txt 文件的字符串列表?
- sql - 查询子集
- html - 关于浮动和清除
- go - 如果设置为丢弃,Golang 记录器的开销
- javascript - 用玩笑在javascript中模拟类原型
- apache-spark - 如何增量加载,适应新数据,使用 spark 保存管道模型?
- mysql - MySQL 错误 1452:无法添加或更新子行
- scala - Scala,猫 - 如何不明确使用 Applicative[F]?