Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Adapters in NextAuth?

I am playing around with next-auth and prisma trying to implement register/login functionality. My schema is

model User {
  id             String    @id @default(auto()) @map("_id") @db.ObjectId
  name           String?
  email          String?   @unique
  hashedPassword String?
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @updatedAt

}

Nextauth options are:

import bcrypt from "bcrypt";
import NextAuth, { AuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
;
import { PrismaAdapter } from "@auth/prisma-adapter";

import client from "@/app/lib/prisma";
import { Adapter } from "next-auth/adapters";

export const authOptions: AuthOptions = {
  adapter: PrismaAdapter(client) as Adapter,
  providers: [

    CredentialsProvider({
      name: "credentials",
      credentials: {
        email: { label: "email", type: "text" },
        password: { label: "password", type: "password" },
      },
      async authorize(credentials) {
        if (!credentials?.email || !credentials?.password) {
          throw new Error("Invalid credentials");
        }

        const user = await client.user.findUnique({
          where: {
            email: credentials.email,
          },
        });

        if (!user || !user?.hashedPassword) {
          throw new Error("Invalid credentials");
        }

        const isCorrectPassword = await bcrypt.compare(
          credentials.password,
          user.hashedPassword
        );

        if (!isCorrectPassword) {
          throw new Error("Invalid credentials");
        }

        return user;
      },
    }),
  ],


const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

I am able to create new user in my database even if remove the adapter line because I am already importing prisma client. My query is why should we use an adapter ? how does it help the developer ?

like image 534
Captainofthe9thdivision Avatar asked Sep 20 '25 12:09

Captainofthe9thdivision


1 Answers

In NextAuth.js, an adapter performs a key role as it serves as a bridge between NextAuth.js and your database, it manages important tasks related to authentication.

When you mention that you can still create a new user in the database even without the adapter, this is because you're directly using the Prisma client. While this lets you interact with your database, it won't take care of managing sessions or OAuth accounts, and it won't integrate with NextAuth.js in the way that using the adapter does.

The adapter takes care of storing a new user in the database when they log in for the first time. If the same user logs in again, the adapter fetches the user's information from the database. Also, if users are logging in through OAuth providers (like GitHub, Google, or Facebook), the adapter links these OAuth accounts to a user account.

like image 138
Nurul Sundarani Avatar answered Sep 23 '25 13:09

Nurul Sundarani