具有蒸汽护照的Angular 6-Auth Guard问题

时间:2018-07-28 15:52:06

标签: node.js angular passport.js steam

我正在使用Steam护照登录。 所以基本上,我去了localhost/auth/steam,这使我重定向到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看来,单击按钮ahah的人不同。

感谢您将来的回答。

编辑: 我进行了一些宣传,当我浏览到/api/isLoggedIn时,以及当我从Angular发出http get请求时,我没有相同的会话,如何将两者链接在一起?

浏览器的会话ID:EvbuWUZ7vceCxHsm7npb6572bLe-1lRC

Angular的会话ID:每次我发出请求时都会更改。.

1 个答案:

答案 0 :(得分:0)

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

查看您的功能。您的函数未返回任何内容。 return语句在异步函数内部。

尝试一下。

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