首页 > 解决方案 > 如何使用 passport-google 策略从节点/express js 验证 Angular 10 客户端应用程序?

问题描述

我正在构建一个在微服务架构之上使用的 Web 应用程序。

使用 node/express js 我已经实现了身份验证服务和产品服务都在监听不同的端口,比如

http://localhost:8001 用于认证服务 http://localhost:8002 用于产品服务。

Kong Gateway 用于使用 jwt 对微服务进行身份验证和连接。实施了 passport-jwt 和 passport-local 策略,以使用 post 调用从客户端对用户进行身份验证。

最后,我在下面的 URL 中使用 passport-google 策略在服务器端实现了 google auth

http://localhost:8001/auth/google -> 登录后它会将我引导到 google auth 同意屏幕,它会重定向到下面的 Url

http://localhost:8001/auth/google/带有令牌的回调。它在服务器端运行良好。

async googlecallback(req, res, next){
   
    passport.authenticate('google', {
        session: false,
    }, (err, user, message) => {  

        if (!user) {
            return next(new UnAuthorizedException(message))
        }
        
        const token = user.generateToken()
        user = UserTransformer.transform(user)
        user.token = token

        this.Response(res, user, message) // sending response to client using custom method
    })(req, res)
}

. 当我从角度应用程序客户端对用户进行身份验证时。我无法继续前进。只是在这里挣扎。:-(

当用户单击客户端角度 10 中的 google 登录按钮时,如何验证用户身份?

我的前端应用程序 URL,例如http://localhost:4002/account/login

尝试使用window.open("http://localhost:8001/auth/google","_blank")方法,但未按预期工作。

res.setHeader('x-code', 'jwthere'); 头方法。还尝试使用 URL 参数传递 JWT 令牌。但两者似乎都不安全。

http://localhost:4002/account/login?token=7wF8bit5W1Pfi5Glt1X8H0YQu8BN7OeNRcX1zbj3AGpUHaYSxLlNIjHpzuw

安全是这里的主要问题。我想要像 khanacademy 社交登录一样的 google 登录

https://www.khanacademy.org

标签: node.jsangularexpresspassport-localpassport-google-oauth2

解决方案


推荐阅读