Add Faable Auth to your Next.js application to easily authenticate your users. This guide shows you how to integrate the Faable Auth login flow using the secure Authorization Code Flow with PKCE, meaning the token exchange happens automatically and securely on the client side.
Prerequisites
Before you start, you need to prepare your Faable Auth environment:
Faable Auth Domain: You need the URL of your Faable Auth instance. You can find this in the Faable Dashboard.
Create a Client: In the dashboard, create a new Client for your application. Note down the Client ID. Make sure you configure your Allowed Callback URLs (e.g., http://localhost:3000/callback for local development).
Installation
Install the required Faable packages in your Next.js project:
First, initialize the Faable Auth client. You can define this directly in your app setup or in a separate file.
import{createClient}from'@faable/auth-js';// Replace with your actual domain and client IDexportconstfaableauth=createClient('YOUR_FAABLE_AUTH_DOMAIN','YOUR_CLIENT_ID');
Adding the Auth Provider
To make the authentication state available throughout your React component tree, wrap your root component (usually pages/_app.tsx or pages/_app.js) with the SessionContextProvider.
Accessing User State
Once the provider is wrapping your application, any component can access the authentication state.
You can use the useSession and useUser hooks to get the current session (which includes the access_token) and the user's profile information.
When you call signInWithOAuth, the @faable/auth-js library takes care of redirecting the user, securely generating the PKCE verifier, and handling the automatic token exchange when the user is sent back to your callback URL.