Next.js Discord

Discord Forum

Dual table authentication setup

Unanswered
Pacific sand lance posted this in #help-forum
Open in Discord
Pacific sand lanceOP
Hello, i'm fairly new here, i want to know how can I configure NextAuth to support authentication options for two distinct database tables (e.g., users and admin/employee) using a Prisma Adapter for database interaction?

23 Replies

Satin Angora
So for this use case maybe it would be best to have a specific table called Account and have an enum of roles

Users (default)
Employee
Admin
Pacific sand lanceOP
yep, i already have such tables
Satin Angora
Use 1 database table with roles.. Depending of course on how these roles are going to be used if these roles are set manually or if you hjave some sort of an upgrade process that might be the simplest and most effective way in this specific instance
Pacific sand lanceOP
so all of that in just one table?
Satin Angora
Yeah it would be an enum with the roles that it can be that would be set in the prisma schema
Pacific sand lanceOP
so it's not really possible for dual or more?
@Pacific sand lance so it's not really possible for dual or more?
Satin Angora
Yeah its possible I am giving you the solution here! 😄
You can also go the multi-table approach as well if you'd like
Let me try and find you some documentation to help you with this
Pacific sand lanceOP
i meant like dual tables, one is a user, one is an admin, the user and the admin tables are totally different
Satin Angora
Would the admin need a user account to be an admin? How would an admin sign up effectively ?
Would it be manually input by you or something along those lines?
@Satin Angora Would the admin need a user account to be an admin? How would an admin sign up effectively ?
Pacific sand lanceOP
nope, it's totally different than the users
the admin will be manually inputted
Satin Angora
Ok yeah so in this case you would do a slight override of the login and how that would work essentially update the callbacks signIn logic

https://next-auth.js.org/configuration/callbacks

 async signIn({ user, account, profile, email, credentials }) {
      //Check to see if the user is 
       const isUser = await prisma.user.findUnique({ where: { email: user.email } });
      const isAdmin = await prisma.admin.findUnique({ where: { email: user.email } });

      //Add conditionals for each set custom cookies/sessions or whatever the case might be
      return true
    },
But I dont really reccomend this approach generally speaking.

I would create a generic account table and have roles within that account and restrict/give access based on the account roles this way we are minimizing the amount of queries for simply logging in by it being part of the data schema in the "Account" table
Pacific sand lanceOP
so just one account table for every roles?
Satin Angora
Yeah so this is just a generic example


model Account {
role Roles
...rest of data types
}

enum Roles {
User
Admin 
Employee
}
Pacific sand lanceOP
ah okay
lemme try this and i'll update it here
@Satin Angora Yeah so this is just a generic example model Account { role Roles ...rest of data types } enum Roles { User Admin Employee }
Pacific sand lanceOP
one more thing, i have a next-auth.d.ts to specify types in my sessions, how do i implement enums? like for example this is what my types look like :
// /types/next-auth.d.ts
import NextAuth from "next-auth/next";

declare module "next-auth" {
    interface User {
        username: string
        fullname: string
        phone: string
        isadmin: boolean
    }
    interface Session {
        user: User & {
            username: string
            fullname: string
            phone: string
            isadmin: boolean
        }
        token: {
            username: string
            fullname: string
            phone: string
            isadmin: boolean
        }
    }
}

i'm going to change the is_admin to role
that role contains the enums you mentioned prior