How to authenticate angular 10 client app from node/express js using passport-google strategy?

374 Views Asked by At

I'm building a web app that is being used on top of microservices architecture.

Using node/express js I have implemented auth service and products service both are listening on different ports like

http://localhost:8001 for authentication service http://localhost:8002 for products service.

Kong Gateway used to authenticate and connect the microservices with jwt. Implemented passport-jwt and passport-local strategy to authenticate the users from client side using post calls.

Finally I have implemented the google auth on server side using passport-google strategy in this below URL

http://localhost:8001/auth/google -> it directs me to google auth consent screen after sign in it is redirecting to below Url

http://localhost:8001/auth/google/callback with token. it works fine at server end.

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)
}

. When I come to authenticate the user from angular app client side. I'm unable to proceed further. just struggling here. :-(

How can I authenticate the user when they click google sign in button in angular 10 on client side?

My front end app Url like http://localhost:4002/account/login

Tried to use window.open("http://localhost:8001/auth/google","_blank") method, not working as expected.

res.setHeader('x-code', 'jwthere'); header method. Also tried to pass the JWT token with URL parameter. but both seems unsecure.

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

security is the major concern here. I want the google sign in like khanacademy social login

https://www.khanacademy.org

0

There are 0 best solutions below