Utilities
The Auth.js client library makes it easy to interact with sessions from React applications.
Example Session Objectβ
{
user: {
name: string
email: string
image: string
},
expires: Date // This is the expiry of the session, not any of the tokens within the session
}
The session data returned to the client does not contain sensitive information such as the Session Token or OAuth tokens. It contains a minimal payload that includes enough data needed to display information on a page about the user who is signed in for presentation purposes (e.g name, email, image).
You can use the session callback to customize the session object returned to the client if you need to return additional data in the session object.
The expires
value is rotated, meaning whenever the session is retrieved from the REST API, this value will be updated as well, to avoid session expiry.
getSession()β
- Client Side: Yes
- Server Side: No (See:
unstable_getServerSession()
Auth.js provides a getSession()
helper which should be called client side only to return the current active session.
On the server side, this is still available to use, however, we recommend using unstable_getServerSession
going forward. The idea behind this is to avoid an additional unnecessary fetch
call on the server side. For more information, please check out this issue.
The unstable_getServerSession
only has the prefix unstable_
at the moment, because the API may change in the future. There are no known bugs at the moment and it is safe to use. If you discover any issues, please do report them as a GitHub Issue and we will patch them as soon as possible.
This helper is helpful in case you want to read the session outside of the context of React.
When called, getSession()
will send a request to /api/auth/session
and returns a promise with a session object, or null
if no session exists.
async function myFunction() {
const session = await getSession()
/* ... */
}
Read the tutorial securing pages and API routes to know how to fetch the session in server side calls using unstable_getServerSession()
.
getCsrfToken()β
- Client Side: Yes
- Server Side: Yes
The getCsrfToken()
method returns the current Cross Site Request Forgery Token (CSRF Token) required to make POST requests (e.g. for signing in and signing out).
You likely only need to use this if you are not using the built-in signIn()
and signOut()
methods.
Client Side Exampleβ
async function myFunction() {
const csrfToken = await getCsrfToken()
/* ... */
}
Server Side Exampleβ
import { getCsrfToken } from "next-auth/react"
export default async (req, res) => {
const csrfToken = await getCsrfToken({ req })
/* ... */
res.end()
}
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 list of the currently configured authentication providers.
It can be useful if you are creating a dynamic custom sign in page.
API Routeβ
import { getProviders } from "next-auth/react"
export default async (req, res) => {
const providers = await getProviders()
console.log("Providers", providers)
res.end()
}
Unlike and getCsrfToken()
, when calling getProviders()
server side, you don't need to pass anything, just as calling it client side.
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β
import { signIn } from "next-auth/react"
export default () => <button onClick={() => signIn()}>Sign in</button>
Starts OAuth sign-in flow when clickedβ
By default, when calling the signIn()
method with no arguments, you will be redirected to the Auth.js sign-in page. If you want to skip that and get redirected to your provider's page immediately, call the signIn()
method with the provider's id
.
For example to sign in with Google:
import { signIn } from "next-auth/react"
export default () => (
<button onClick={() => signIn("google")}>Sign in with Google</button>
)
Starts Email sign-in flow when clickedβ
When using it with the email flow, pass the target email
as an option.
import { signIn } from "next-auth/react"
export default ({ email }) => (
<button onClick={() => signIn("email", { email })}>Sign in with Email</button>
)
Specifying a callbackUrl
β
The callbackUrl
specifies to which URL the user will be redirected after signing in. Defaults to the page URL the sign-in is initiated from.
You can specify a different callbackUrl
by specifying it as the second argument of signIn()
. This works for all providers.
e.g.
signIn(undefined, { callbackUrl: '/foo' })
signIn('google', { callbackUrl: 'http://localhost:3000/bar' })
signIn('email', { email, callbackUrl: 'http://localhost:3000/foo' })
The URL must be considered valid by the redirect callback handler. By default it requires the URL to be an absolute URL at the same host name, or a relative url starting with a slash. If it does not match it will redirect to the homepage. You can define your own redirect callback to allow other URLs.
Using the redirect: false
optionβ
The redirect option is only available for credentials
and email
providers.
In some cases, you might want to deal with the sign in response on the same page and disable the default redirection. For example, if an error occurs (like wrong credentials given by the user), you might want to handle the error on the same page. For that, you can pass redirect: false
in the second parameter object.
e.g.
signIn('credentials', { redirect: false, password: 'password' })
signIn('email', { redirect: false, email: 'bill@fillmurray.com' })
signIn
will then return a Promise, that resolves to the following:
{
/**
* Will be different error codes,
* depending on the type of error.
*/
error: string | undefined
/**
* HTTP status code,
* hints the kind of error that happened.
*/
status: number
/**
* `true` if the signin was successful
*/
ok: boolean
/**
* `null` if there was an error,
* otherwise the url the user
* should have been redirected to.
*/
url: string | null
}
Additional parametersβ
It is also possible to pass additional parameters to the /authorize
endpoint through the third argument of signIn()
.
See the Authorization Request OIDC spec for some ideas. (These are not the only possible ones, all parameters will be forwarded)
e.g.
signIn("identity-server4", null, { prompt: "login" })
always ask the user to re-authenticatesignIn("auth0", null, { login_hint: "info@example.com" })
hints the e-mail address to the provider
You can also set these parameters through provider.authorizationParams
.
The following parameters are always overridden server-side: redirect_uri
, state
signOut()β
- Client Side: Yes
- Server Side: No
In order to logout, use the signOut()
method to ensure 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.
import { signOut } from "next-auth/react"
export default () => <button onClick={() => signOut()}>Sign out</button>
Specifying a callbackUrl
β
As with the signIn()
function, you can specify a callbackUrl
parameter by passing it as an option.
e.g. signOut({ callbackUrl: 'http://localhost:3000/foo' })
The URL must be considered valid by the redirect callback handler. By default, it requires the URL to be an absolute URL at the same host name, or you can also supply a relative URL starting with a slash. If it does not match it will redirect to the homepage. You can define your own redirect callback to allow other URLs.
Using the redirect: false
optionβ
If you pass redirect: false
to signOut
, the page will not reload. The session will be deleted, and the useSession
hook is notified, so any indication about the user will be shown as logged out automatically. It can give a very nice experience for the user.
If you need to redirect to another page but you want to avoid a page reload, you can try:
const data = await signOut({redirect: false, callbackUrl: "/foo"})
where data.url
is the validated URL you can redirect the user to without any flicker by using Next.js's useRouter().push(data.url)