首页 > 解决方案 > 带有蒸汽护照的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:每次我提出请求时它都会改变..

标签: node.jsangularpassport.jssteam

解决方案


您的isAuthenticated()函数始终返回undefined(可以视为错误)。

看看你的功能。你的函数没有返回任何东西。return 语句在 async 函数内部。

尝试这个。

isAuthenticated(): Observable<boolean> {
    return this.http.get('http://localhost:8080/api/isLoggedIn')
      .pipe(
        map(data => !!JSON.parse(data._body).success)
      )
  }

推荐阅读