thisismydesign
thisismydesign

Reputation: 25172

OAuth2 flow in full-stack NestJS application

Yet another OAuth2 question that isn't quite covered elsewhere.

I'm using NestJS backend, React frontend, Passport and my own DB for authentication. Trying to add an OAuth2 identity provider (Google).

I configured my NestJS app as an OAuth Client. After logging in I'm receiving the callback at which point I have an access_token and the requested user details extracted from the payload of the id_token. This is encapsulated in a class extending PassportStrategy(Strategy, 'google') and an AuthGuard('google') and much of it is handled automatically. Code here.

At this point however, I need to maintain an authenticated session between backend (NestJS) and frontend (React). I suppose I need a JWT, but I'm wondering about the best approach:

One drawback here is the extra network request every time. I'm not sure if there's a need for that because the IdP is only used to identify the user, not for access to other resources. Another drawback is that I need to store a refresh token and handle when the token expires (get new one, update it on the frontend).

Upvotes: 6

Views: 12662

Answers (2)

mehdi parastar
mehdi parastar

Reputation: 827

probably my solution would be helpful.

you could access complete source code of my app that implemented with react typescript (redux toolkit rtk Query) and nestjs included of google oauth2 flow with passport.js. resource

Upvotes: 0

thisismydesign
thisismydesign

Reputation: 25172

I ended up issuing my own JWT tokens and managing User sessions in my app, as described in this article: OAuth2 in NestJS for Social Login (Google, Facebook, Twitter, etc)

Upvotes: 2

Related Questions