aidotaosm / server-side-authentication-in-nextjs-with-next-auth-and-react-query

Server Side authentication flow with next-auth Credentials provider and react-query.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Server Side authentication flow with Next.js and TypeScript.

This uses next-auth "Credentials" provider which uses httpOnly JWT (JWE) token for sessions.

React-query and Jotai is used for automatic accessToken renewal using the refresh token.

Uses axios interceptor "Component" to refresh the accessToken (accessToken expires => 401 => refresh accessToken from server=> update state and retry the failed request(s)) as a fallback if automatic refresh fails.

Server session (states) are managed with react-query (Prefetch the query (session) on the server, dehydrate the cache and rehydrate it on the client) for instant authenticated state upon refreshes.

About

Server Side authentication flow with next-auth Credentials provider and react-query.


Languages

Language:TypeScript 100.0%