2.0.0 • Published 6 months ago

jwt-auth-next v2.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

jwt-next-auth v2.0.0

Implement JWT authentication using NextJS (>= v14) middleware and API routes.

How to use

Create a lib folder with a file called jwtAuthClient.ts and call the createAuthClient function.

// lib/jwtAuthClient.ts

import { createJWTAuthClient } from 'jwt-auth-next';

export default createJWTAuthClient({
    // ... options
});

Create an API route in the app directory to handle refreshing the access token if required.

// app/api/auth/refresh-token/route.ts

import jwtAuthClient from '@/lib/jwtAuthClient';

const handler = jwtAuthClient.handlers.validateTokenCookiesHandler;

export { handler as POST };

Create a middleware.ts file in the root directory and add the following:

// middleware.ts

import { NextRequest, NextResponse } from 'next/server';
import jwtAuthClient from './lib/jwtAuthClient';

export async function middleware(req: NextRequest) {
    const res = NextResponse.next();

    return jwtAuthClient.handlers.refreshTokenMiddleware(
        { req, res },
        {
            // ... event handlers
        }
    );
}

export const config = {
    matcher: [
        // ... all routes EXCEPT 'api/**/*'
    ],
};
2.0.0

6 months ago

1.1.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago

1.0.0-beta.3

7 months ago

1.0.0-beta.2

7 months ago

1.0.0-beta.1

7 months ago