Client API
The NextAuth.js client library makes it easy to interact with sessions from React applications.
Some of the methods can be called both client side and server side.
note
When using any of the client API methods server side, context must be passed as an argument. The documentation for getSession() has an example.
useSession()
- Client Side: Yes
- Server Side: No
The useSession() React Hook in the NextAuth.js client is the easiest way to check if someone is signed in.
It works best when used with NextAuth.js <Provider> is added to pages/_app.js (see provider).
getSession()
- Client Side: Yes
- Server Side: Yes
NextAuth.js also provides a getSession() method which can be called client or server side to return a session.
It calls /api/auth/session and returns a promise with a session object, or null if no session exists.
A session object looks like this:
You can call getSession() inside a function to check if a user is signed in, or use it for server side rendered pages that supporting signing in without requiring client side JavaScript.
info
Note that because it exposed to the client it does not contain sensitive information such as the Session Token or OAuth service related tokens. It includes enough information (e.g name, email) to display information on a page about the user who is signed in, and an Access Token that can be used to identify the session without exposing the Session Token itself.
Because it is a Universal method, you can use getSession() in both client and server side functions, such as getInitialProps() in Next.js:
Using getSession() in API routes
You can also get the session object in Next.js API routes:
note
When calling getSession() server side, you must pass the request object - e.g. getSession({req}) - or you can the pass entire context object as it contains the req object.
getProviders()
- Client Side: Yes
- Server Side: Yes
The getProviders() method returns the list of providers currently configured for sign in.
It calls /api/auth/providers and returns a with a list of the currently configured authentication providers.
It can be use useful if you are creating a dynamic custom sign in page.
getCsrfToken()
- Client Side: Yes
- Server Side: Yes
The getCsrfToken() method returns the current Cross Site Request Forgery (CSRF Token) required to make POST requests (e.g. for signing in and signing out). It calls /api/auth/csrf.
You likely only need to use this if you are not using the built-in signin() and signout() methods.
signin()
- Client Side: Yes
- Server Side: No
Using the signin() method ensures the user ends back on the page they started on after completing a sign in flow. It will also handle CSRF tokens for you automatically when signing in with email.
The signin() method can be called from the client in different ways, as shown below.
Redirects to sign in page when clicked
Starts Google OAuth sign-in flow when clicked
Starts Email sign-in flow when clicked
When using it with the email flow, pass the target email as an option.
tip
To also support signing in from clients that do not have client side JavaScript, use a regular link, add an onClick handler to it and call e.preventDefault() before calling the signin() method.
signout()
- Client Side: Yes
- Server Side: No
Using the signout() method ensures the user ends back on the page they started on after completing the sign out flow. It also handles CSRF tokens for you automatically.
It reloads the page in the browser when complete.
Provider
Using the supplied React <Provider> allows instances of useSession() to share the session object across components, buy using React Context under the hood.
This improves performance, reduces network calls and avoids page flicker when rendering.
It is highly recommended and can be easily added to all pages in Next.js apps by using /pages/_app.js.
If you pass the session page prop to the <Provider> – as in the example above – you can avoid checking the session twice on pages that support both server and client side rendering.
note
See the Next.js documentation for more information on _app.js in Next.js applications.