setup done

This commit is contained in:
Om Lanke
2025-06-25 15:25:44 +05:30
parent ceb4b6b811
commit 2b777df5e2
55 changed files with 503 additions and 90 deletions

View File

@@ -1,9 +1,65 @@
import NextAuth, { type NextAuthConfig } from "next-auth";
import Google from "next-auth/providers/google";
import { db, admins, students } from "@workspace/db";
import { eq } from "drizzle-orm";
const authConfig: NextAuthConfig = {
providers: [Google],
callbacks: {},
callbacks: {
async jwt({ token, account, user, profile }) {
// Only check DB on first sign in
if (account && user && user.email) {
const admin = await db
.select()
.from(admins)
.where(eq(admins.email, user.email))
.limit(1);
if (admin.length > 0 && admin[0]) {
token.role = "ADMIN";
token.adminId = admin[0].id;
} else {
token.role = "USER";
const student = await db
.select()
.from(students)
.where(eq(students.email, user.email))
.limit(1);
if (student.length > 0 && student[0]) {
token.studentId = student[0].id;
} else {
const nameParts = user.name?.split(" ") ?? [];
const firstName = nameParts[0] || "";
const lastName = nameParts.slice(1).join(" ") || "";
const newStudent = await db
.insert(students)
.values({
email: user.email,
firstName: firstName,
lastName: lastName,
profilePicture: user.image,
})
.returning({ id: students.id });
if (newStudent[0]) {
token.studentId = newStudent[0].id;
}
}
}
}
return token;
},
async session({ session, token }) {
if (token?.role) {
session.user.role = token.role as "ADMIN" | "USER";
}
if (token?.adminId) {
session.user.adminId = token.adminId as number;
}
if (token?.studentId) {
session.user.studentId = token.studentId as number;
}
return session;
},
},
};
const nextAuth = NextAuth(authConfig);

View File

@@ -12,7 +12,9 @@
"license": "ISC",
"packageManager": "pnpm@10.4.1",
"dependencies": {
"next-auth": "5.0.0-beta.28"
"next-auth": "5.0.0-beta.28",
"@workspace/db": "workspace:*",
"drizzle-orm": "^0.44.2"
},
"devDependencies": {
"dotenv": "^16.5.0"

20
packages/auth/types.d.ts vendored Normal file
View File

@@ -0,0 +1,20 @@
import "next-auth";
import "next-auth/jwt";
declare module "next-auth" {
interface Session {
user: {
role?: "ADMIN" | "USER";
adminId?: number;
studentId?: number;
[key: string]: any;
};
}
}
declare module "next-auth/jwt" {
interface JWT {
role?: "ADMIN" | "USER";
adminId?: number;
studentId?: number;
}
}