Next Handler

The Next Handler provides a function to manage sessions in your Next.js application.

  • Sessions can't be created or deleted from server components.
  • Use it within server actions or API routes.
  • Other auth methods can be used anywhere, including server components, server actions, or API routes.

How to Use

Follow these steps to integrate the Next Handler into your application:

  1. Import Required Modules:

Make sure you import the necessary modules and adapters.

src/auth.ts
import mongoose from "mongoose"
import { NextHandler } from "authy-js/handlers/next"
import { MongoDBAdapter } from "authy-js/adapters/mongodb"
 
// Import your MongoDB models
import UserModel from "./models/user.model"
import SessionModel from "./models/session.model"
  1. Set Up MongoDB Adapter:

Initialize the MongoDB Adapter with your User and Session models. Refer to the MongoDB Adapter for more details.

src/auth.ts
const mongodbAdapter = new MongoDBAdapter({
    User: UserModel,
    Session: SessionModel,
    dbConnectionUrl: "mongodb://localhost:27017/YourDatabase",
    mongoose: mongoose,
})
  1. Initialize the NextHandler:

Set up the NextHandler with your adapter and optional cookie settings.

src/auth.ts
// `auth` has all the necessary methods
export const auth = NextHandler({ adapter: mongodbAdapter, cookie: {} })
src/auth.ts
// or you can provide your user and session types to get suggestions
export const auth = NextHandler<IUser, ISession>({ adapter: mongodbAdapter, cookie: {} })
  1. Examples using server actions:

Here are some examples of how to use the auth function in your Next.js application.

src/actions.ts
"use server"
 
import { auth } from "@/auth"
 
export async function create() {
    const expiresAt = new Date(Date.now() + 30000) // Expires in 30 seconds
    // Can't call it directly inside server components
    return await auth.createSession({ userId: "yourUserId", expiresAt })
}
 
export async function getUser() {
    // Can be called directly inside server components
    return await auth.getCurrentUser()
}
 
export async function logout() {
    // Can't call it directly inside server components
    return await auth.deleteCurrentSession()
}

Methods

  • createSession(): Creates a new session in the database.

  • getCurrentSession(): Retrieves details of the current session.

  • getCurrentUser(): Retrieves details of the authenticated user.

  • deleteCurrentSession(): Deletes the current session.

  • deleteUsersAllSessions(): Deletes all sessions associated with the current user.

With these steps, you can easily manage sessions in your Next.js application using the Next Handler.