I have a NestJS backend that exposes the following API:
@Post('sign-in-with-google-account')
async signInWithGoogleAccount(
@Body body: { idToken: string },
@Res({ passthrough: true }) response: Response
) {
const user = await getUserFromGoogleIdToken(body.idToken)
const tokens = await generateAccessAndRefreshTokensForUser(user)
response.cookie('refreshToken', tokens.refreshToken, {
httpOnly: true,
expires: new Date(tokenExpirationDate),
secure: true,
sameSite: 'none'
})
return { accessToken: tokens.accessToken }
}
It receives id token from google oauth, finds the user in the DB and signs a JWT access token and refresh token. The refresh token is stored as httpOnly
cookie and the access token is returned.
Now in my next.js app configured with next-auth
I have the following:
import GoogleProvider from "next-auth/providers/google";
...
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
})
]
...
The problem is that next-auth
generates its own tokens. But I want next-auth
to use my own access and refresh tokens from the NestJS backend, how can I do that?
Also, In NestJS I have a API to refresh the access token like so:
@Get('refresh-access-token')
async refreshAccessToken(@Req() request: Request) {
const accessToken = await getNewAccessTokenFromRefreshToken(request.cookies.refreshToken)
return { accessToken }
}
How can I tell next-auth
to refresh the access token using refresh-access-token
API every 10 minutes (the access token expiration date)?
next-auth
to use the tokens generated by my custom backend. Not the one created by next-auth itself. I want to integrateGoogle Login
andCredentials Provider
– Eusporangiate