Next.js Quickstart
This is a quickstart guide to help you integrate Descope with your Next.js application. Follow the steps below to get started.
Import and Wrap Application with AuthProvider
Wrap the entire application with <AuthProvider />
. You need your Project ID for this step, which you can find on the project page of your Descope console.
Note
You can also add the optional baseUrl
parameter if you're utilizing a CNAME within your Descope project (ex: https://auth.company.com
).
Import Descope Functions and Add Flows Component
To trigger the Descope Flow, you will need to add this component. The screens you've customized in your Flow will appear here. You can also customize the component with the following:
flowId
: ID of the flow you wish to useonSuccess
andonError
: functions that execute when authentication succeeds or fails
Note
For the full list of component customization options, refer to our Descope Components Customization Doc.
Utilize the NextJS SDK Hooks and Functions
Descope provides many different hooks to check if the user is authenticated, session is loading etc. You can use these to customize the user experience:
isAuthenticated
: Boolean for the authentication state of the current user session.isSessionLoading
: Boolean for the loading state of the session. Can be used to display a "loading" message while the session is still loading.useUser
: Returns information about the currently authenticated user.sessionToken
: The JWT for the current session.
Note
For the full list of available hooks and functions, refer to the Auth Helpers Doc.
Add Logout Functionality
You can use the React useCallback hook to create your logout function.
useDescope()
: Returns further operations related to authentication including logout.
Setting Up the Middleware
You can use NextJS Middleware to require authentication for specific pages and routes in your application. The Descope SDK provides a middleware function that can be used to require authentication for your protected pages and routes. Read more about the Next SDK here
Congratulations
Now that you've got the authentication down, go focus on building out the rest of your app!
Checkpoint
Your application is now integrated with Descope. Please test with sign-up or sign-in use case.
Customize
Now that you have the end-to-end application working, you can choose to configure and personalize many different areas of Descope, including your brand, style, custom user authentication journeys, etc. We recommend starting with customizing your user-facing screens, such as signup and login.