Tesseral for Next.js
Tesseral’s Next.js SDK lets you add authentication to your Next.js application.
The Tesseral Next.js SDK is open-source and available on GitHub.
Getting Started
Sign up for Tesseral
Sign up for Tesseral at https://console.tesseral.com and create your project.
When signing up, you’ll be asked for your development URL. Choose
http://localhost:3000
.
Install @tesseral/tesseral-nextjs
In your Next.js codebase, add @tesseral/tesseral-nextjs
to your project:
Add NEXT_PUBLIC_TESSERAL_PUBLISHABLE_KEY
to your .env
Add the following line to your .env
file:
Replace publishable_key_...
with your project’s Publishable Key. You get can
your Publishable Key from the API Keys
Settings settings in
the Tesseral Console.
Add authMiddleware
to your middleware.ts
Create a middleware.ts
(or src/middleware.ts
if you’re using the src
directory) and make its contents be:
Add TesseralProvider
to your root layout
Go to app/layout.tsx
(or src/app/layout.tsx
if you use the src
directory)
and add TesseralProvider
:
TesseralProvider
will redirect users to log in if they aren’t already. If your
Next.js app serves both public and non-public pages, then add TesseralProvider
at the layout for your non-public pages instead.
You’ve now added Tesseral to your Next.js application! From here, you can add auth checks to your:
Server Components (RSCs)
In Next.js
layouts and
pages, you
can get the current User, and the
Organization they work for, with
await getUser()
and await getOrganization()
.
getUser
To get the current User from a Server Component (RSC),
use await getUser()
:
getOrganization
To get the current Organization from a Server
Component (RSC), use await getOrganization()
:
getUserSettingsUrl
Tesseral automatically provides a self-serve user settings UI to handle tasks like changing emails, resetting passwords, and configuring multi-factor authentication.
You can get a link to that UI using getUserSettingsUrl
:
getOrganizationSettingsUrl
Tesseral automatically provides a self-serve organization settings UI to handle tasks like managing collaborators, creating and revoking user invites, and customizing login methods.
You can get a link to that UI using getOrganizationSettingsUrl
:
Client Components
In Next.js
Client Components,
you can get the current User, and the
Organization they work for, with
useUser()
and useOrganization()
.
useUser
To get the current User from a Client Component, use the
useUser()
hook:
useOrganization
To get the current Organization from a Client
Component, use the useOrganization()
hook:
useHasPermission
To use useHasPermission
, you’ll first need to set up Role-Based Access
Control.
To check whether a user has permission to do an
Action from a Client
Component, use useHasPermission
:
hasPermission
is a function that takes a string
and returns a boolean
.
useHasPermission
is only for “look before you leap” use-cases. Only
server-side code can ultimately enforce permissions.
To enforce permission checks from the server, see hasPermission
from await auth()
.
useLogout
To force the current User to log out from a Client
Component, use the useLogout()
hook:
useUserSettingsUrl
Tesseral automatically provides a self-serve user settings UI to handle tasks like changing emails, resetting passwords, and configuring multi-factor authentication.
You can get a link to that UI using useUserSettingsUrl
:
useOrganizationSettingsUrl
Tesseral automatically provides a self-serve organization settings UI to handle tasks like managing collaborators, creating and revoking user invites, and customizing login methods.
You can get a link to that UI using useOrganizationSettingsUrl
:
Route Handlers
To get authentication details about the current request in a
Route Handler,
call await auth()
:
Server Actions
To get authentication details about the current request in a Server Action, you can use the same methods as in Server Components (RSCs):
actions.ts
page.tsx
Using await auth()
The auth()
function from Tesseral’s Next.js SDK works with both
Users and API Keys.
That’s why auth()
is recommended for
Next.js Route Handlers. You should also use auth()
from
code that gets called from both Server Components and Route Handlers.
auth()
parameters
auth()
takes one parameter, called or
. or
controls what happens if a
request is not properly authenticated. It has three possible values:
-
await auth({ or: "throw" })
will throw aAuthError
if the request is not properly authenticated. Use this if you want to have different behavior based on authenticated vs unauthenticated requests, usingtry
/catch
. -
await auth({ or: "redirect" })
will redirect to the login page if the request is not properly authenticated. Use this for pages that are only meant to be used Users from their web browser. -
await auth({ or "return_404" })
will call Next.js’snotFound()
if the request is not properly authenticated. Use this if a 404 is the appropriate response for unauthenticated requests.
In all cases, you can always safely
destructure
the return value of await auth()
.
await auth()
return value
await auth()
returns an object with properties you can destructure:
Getting the current Organization
To access the Organization the request is for from await auth()
, use the
organizationId
property:
This is the most common identifier you’ll use in a B2B SaaS application.
Getting the request’s authenticated credentials
Anywhere your code wants to forward along the request’s credentials, use
credentials
:
Do not log or expose this value. You usually don’t need to use this unless you’re building internal service-to-service calls.
Getting the type of credentials used
The credentialsType
property is a string indicating how the request was
authenticated. It will either be:
Getting details about the current User
If the request is from a User, then accessTokenClaims
will contain details about that User. If the request is from an
API Key, then accessTokenClaims
will be
undefined
.
We recommend that you mostly use
organizationId
in the vast majority of
your code; that is almost always the correct piece of information for most B2B
SaaS code should pay attention to. For more details, see
B2B Multitenancy.
Checking the requester’s permissions
To use hasPermission
, you’ll first need to set up Role-Based Access
Control.
To check whether a user has permission to do an
Action, use hasPermission
:
hasPermission
is a function that takes a string
and returns a boolean
.