react
NextAuth.js methods and components that work in Client components and the Pages Router.
For use in Server Actions, check out these methods
SessionProvider()β
SessionProvider(
props
):Element
React Context provider to wrap the app (pages/
) to make session data available anywhere.
When used, the session state is automatically synchronized across all open tabs/windows and they are all updated whenever they gain or lose focus
or the state changes (e.g. a user signs in or out) when SessionProviderProps.refetchOnWindowFocus is true
.
You will likely not need SessionProvider
if you are using the Next.js App Router (app/
).
Parametersβ
βͺ props: SessionProviderProps
Returnsβ
Element
getCsrfToken()β
getCsrfToken():
Promise
<string
>
Returns the current Cross-Site Request Forgery Token (CSRF Token) required to make requests that changes state. (e.g. signing in or out, or updating the session).
CSRF Prevention: Double Submit Cookie
Returnsβ
Promise
< string
>
getProviders()β
getProviders():
Promise
<null
|ProvidersType
>
Returns a client-safe configuration object of the currently available providers.
Returnsβ
Promise
< null
| ProvidersType
>
signIn()β
signIn<
P
>(provider
?,options
?,authorizationParams
?):Promise
<P
extendsRedirectableProviderType
?SignInResponse
|undefined
:undefined
>
Initiate a signin flow or send the user to the signin page listing all possible providers. Handles CSRF protection.
Type parametersβ
βͺ P extends undefined
| RedirectableProviderType
= undefined
Parametersβ
βͺ provider?: LiteralUnion
< P
extends RedirectableProviderType
? BuiltInProviderType
| P
: BuiltInProviderType
>
βͺ options?: SignInOptions
βͺ authorizationParams?: SignInAuthorizationParams
Returnsβ
Promise
< P
extends RedirectableProviderType
? SignInResponse
| undefined
: undefined
>
signOut()β
signOut<
R
>(options
?):Promise
<R
extendstrue
?undefined
:SignOutResponse
>
Initiate a signout, by destroying the current session. Handles CSRF protection.
Type parametersβ
βͺ R extends boolean
= true
Parametersβ
βͺ options?: SignOutParams
< R
>
Returnsβ
Promise
< R
extends true
? undefined
: SignOutResponse
>
useSession()β
useSession<
R
>(options
?):SessionContextValue
<R
>
React Hook that gives you access to the logged in user's session data and lets you modify it.
You will likely not need useSession
if you are using the Next.js App Router (app/
).
Type parametersβ
βͺ R extends boolean
Parametersβ
βͺ options?: UseSessionOptions
< R
>
Returnsβ
SessionContextValue
< R
>
SessionContextValue<R>β
SessionContextValue<
R
>:R
extendstrue
?object
|object
:object
|object
useSession() returns an object containing three things: a method called update, data
and status
.
Type parametersβ
βͺ R extends boolean
= false
UpdateSessionβ
UpdateSession: (
data
?) =>Promise
<Session
|null
>
Parametersβ
βͺ data?: any
Returnsβ
Promise
< Session
| null
>
Todoβ
Document
SessionProviderPropsβ
If you have session expiry times of 30 days (the default) or more, then you probably don't need to change any of the default options.
However, if you need to customize the session behavior and/or are using short session expiry times, you can pass options to the provider to customize the behavior of the useSession hook.
Propertiesβ
refetchIntervalβ
refetchInterval?:
number
A time interval (in seconds) after which the session will be re-fetched.
If set to 0
(default), the session is not polled.
refetchOnWindowFocusβ
refetchOnWindowFocus?:
boolean
SessionProvider
automatically refetches the session when the user switches between windows.
This option activates this behaviour if set to true
(default).
refetchWhenOfflineβ
refetchWhenOffline?:
false
Set to false
to stop polling when the device has no internet access offline (determined by navigator.onLine
)