FeaturesAuthentication
PreviousNext

Integration

How to implement the authentication feature in an existing application.

This guide explains how to implement the authentication feature in a new/existing application using the @kit/auth package.

Installation

First, install the authentication package:

pnpm add '@kit/auth@workspace:*'

Configuration File

Create the authentication configuration in config/auth.config.ts.

This object will contain all customization options, allowing you to change the behavior of the authentication system.

You gonna pass this object to almost all the code coming from the @kit/auth package.

config/auth.config.ts
import { parseAuthConfig } from '@kit/auth/config'; export const authConfig = parseAuthConfig({ scopePatterns: { auth: '/auth/*?', private: '/your-protected-website-endpoints/*?', }, urls: { home: '/your-protected-website-endpoints', callback: '/auth/callback', signIn: '/auth/sign-in', signUp: '/auth/sign-up', forgottenPassword: '/auth/forgotten-password', verifyMfa: '/auth/verify-mfa', }, captchaSiteKey: process.env.NEXT_PUBLIC_CAPTCHA_SITE_KEY, displayTermsCheckbox: true, providers: { password: true, oAuth: ['google'], }, passwordRequirements: { minLength: 3, maxLength: 99, specialChars: false, numbers: false, uppercase: false, }, });

AuthConfig

PropTypeDefault
scopePatterns*
{ auth: string; private:...
providers*
{ password: boolean; oAuth:...
urls*
{ dashboard: string;...
captchaSiteKey
string
displayTermsCheckbox
boolean
passwordRequirements
{ minLength: number;...

Middleware

middleware.ts
import { authMiddleware } from '@kit/auth/middleware/auth';
import { NextResponse, type NextRequest } from 'next/server';
import { authConfig } from './config/auth.config';
 
export async function middleware(request: NextRequest): Promise<NextResponse<unknown>> {
    const response = NextResponse.next();
 
    const authResponse = await authMiddleware(request, response, authConfig);
    if (authResponse) return authResponse;
 
    // ...other middleware logic
 
    return response;
}
 
export const config = {
    matcher: ['/((?!_next/static|_next/image|images|locales|assets|api/*).*)'],
};

Auth Provider

You must wrap your app with the AuthProvider component.

app/layout.tsx
import React from 'react';
import { AuthProvider } from '@kit/auth/ui/auth-provider';
import { authConfig } from '~/config/auth.config';
 
export default function Layout({ children }: React.PropsWithChildren) {
    
    return (
        <html>
            <body>
                <AuthProvider authConfig={authConfig}>
                    {children}
                </AuthProvider>
            </body>
        </html>
    );
}
 

AuthProviderProps

PropTypeDefault
authConfig*
{ scopePatterns: { auth:...
onEvent
function
children
ReactNode

Auth Pages

Now you need to create the authentication pages in a app/auth/ directory.

To simplify as much as possible the integration, we created the following helper functions to generate the authentication pages:

FunctionDescription
createSignInPage (authConfig, joinOrganizationPath?)Generates the sign in page
createSignUpPage (authConfig)Generates the sign up page
createForgottenPasswordPage (authConfig)Generates the forgotten password page
createVerifyMfaPage (authConfig)Generates the MFA verification page

You just gonna need to pass the authConfig object to the helper function to generate the page.

Example

app/auth/sign-in/page.tsx
import { createSignInPage } from '@kit/auth/pages/sign-in';
import { authConfig } from '~/config/auth.config';
 
export default createSignInPage(authConfig, '/dashboard');

Now do the same for the other pages:

  • app/auth/sign-up/page.tsx
  • app/auth/forgotten-password/page.tsx
  • app/auth/verify-mfa/page.tsx

Auth Routes

Same thing, we created the following helper functions to generate the authentication routes:

FunctionDescription
createCallbackRoute(authConfig)Generates the callback route used at the end of sign in and sign up flows
createConfirmRoute(authConfig)Generates the confirm route for the MFA flow

Example

Here is what the app/auth/callback/route.ts file will look like:

app/auth/callback/route.ts
import { createCallbackRoute } from '@kit/auth/routes/callback';
import { authConfig } from '~/config/auth.config';
 
export const GET = createCallbackRoute(authConfig);

Now do the same for the other route:

  • app/auth/confirm/route.ts

Setup your authentication providers

You still need to set your environment variables and the supabase authentication providers.

Follow the previous page to setup your authentication provider.

What's Next

You've successfully integrated the core authentication system into your application. The integration guide has covered:

Now you users will be able to sign in and sign up to your application.

But you still need to get the user data and handle the user logout to have a fully working authentication system.

In the next pages, you will learn how to:

PageDescription
User AccessLearn how to retrieve, display user information and logout a user
Password ManagementChange password functionality and security features
Session ManagementView and manage active user sessions
MFA SecurityEnable two-factor authentication
Account DeletionDelete a user account

How is this guide?

Last updated on 10/17/2025