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;
}
}

View File

@@ -10,11 +10,11 @@ const buttonVariants = cva(
variants: {
variant: {
default:
"bg-primary text-primary-foreground shadow-xs hover:bg-primary/90",
"bg-primary text-primary-foreground shadow-xs shadow-foreground/20 hover:bg-primary/90",
destructive:
"bg-destructive text-white shadow-xs hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40",
"bg-destructive text-destructive-foreground shadow-xs shadow-foreground/20 hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40",
outline:
"bg-background border border-border text-foreground shadow transition-all duration-300 ease-out group relative overflow-hidden hover:bg-muted hover:border-muted-foreground hover:shadow-lg hover:shadow-primary/20 hover:scale-102 active:scale-98 active:shadow-md focus:ring-2 focus:ring-primary focus:ring-offset-2",
"bg-background border border-border text-foreground shadow shadow-foreground/20 transition-all duration-300 ease-out group relative overflow-hidden hover:bg-muted hover:border-muted-foreground hover:shadow-lg hover:shadow-primary/20 hover:scale-103 active:scale-98 active:shadow-md focus:ring-2 focus:ring-primary focus:ring-offset-2",
secondary:
"bg-secondary text-secondary-foreground shadow-xs hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",

View File

@@ -5,77 +5,82 @@
@import "tw-animate-css";
@theme {
--font-sans: "var(--font-sans)";
--font-marcellus: "var(--font-marcellus)";
}
@custom-variant dark (&:is(.dark *));
:root {
--background: oklch(1 0 0);
--foreground: oklch(0.145 0 0);
--card: oklch(1 0 0);
--card-foreground: oklch(0.145 0 0);
--background: oklch(0.9889 0.004 286.33);
--foreground: oklch(0.2442 0.006 0.59);
--card: oklch(0.97 0.004 286.33);
--card-foreground: oklch(0.2442 0.006 0.59);
--popover: oklch(1 0 0);
--popover-foreground: oklch(0.145 0 0);
--primary: oklch(0.205 0 0);
--primary-foreground: oklch(0.985 0 0);
--secondary: oklch(0.97 0 0);
--secondary-foreground: oklch(0.205 0 0);
--muted: oklch(0.97 0 0);
--muted-foreground: oklch(0.556 0 0);
--accent: oklch(0.97 0 0);
--accent-foreground: oklch(0.205 0 0);
--destructive: oklch(0.577 0.245 27.325);
--destructive-foreground: oklch(0.577 0.245 27.325);
--border: oklch(0.922 0 0);
--input: oklch(0.922 0 0);
--ring: oklch(0.708 0 0);
--chart-1: oklch(0.646 0.222 41.116);
--chart-2: oklch(0.6 0.118 184.704);
--chart-3: oklch(0.398 0.07 227.392);
--chart-4: oklch(0.828 0.189 84.429);
--chart-5: oklch(0.769 0.188 70.08);
--popover-foreground: oklch(0.2442 0.006 0.59);
--primary: oklch(0.5044 0.184 22.51);
--primary-foreground: oklch(0.9889 0.004 286.33);
--secondary: oklch(0.4605 0.005 271.31);
--secondary-foreground: oklch(0.9889 0.004 286.33);
--muted: oklch(0.92 0.003 286.33);
--muted-foreground: oklch(0.44 0.004 286.33);
--accent: oklch(0.6034 0.234 27.23);
--accent-foreground: oklch(0.9889 0.004 286.33);
--destructive: oklch(0.55 0.18 27.23);
--destructive-foreground: oklch(0.9889 0.004 286.33);
--border: oklch(0.88 0.003 286.33);
--input: oklch(0.97 0.004 286.33);
--ring: oklch(0.6034 0.234 27.23);
--chart-1: oklch(0.5044 0.184 22.51);
--chart-2: oklch(0.6034 0.234 27.23);
--chart-3: oklch(0.4605 0.005 271.31);
--chart-4: oklch(0.7 0.15 120);
--chart-5: oklch(0.7 0.15 300);
--radius: 0.625rem;
--sidebar: oklch(0.985 0 0);
--sidebar-foreground: oklch(0.145 0 0);
--sidebar-primary: oklch(0.205 0 0);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.97 0 0);
--sidebar-accent-foreground: oklch(0.205 0 0);
--sidebar-border: oklch(0.922 0 0);
--sidebar-ring: oklch(0.708 0 0);
--sidebar: oklch(0.97 0.004 286.33);
--sidebar-foreground: oklch(0.2442 0.006 0.59);
--sidebar-primary: oklch(0.5044 0.184 22.51);
--sidebar-primary-foreground: oklch(0.9889 0.004 286.33);
--sidebar-accent: oklch(0.6034 0.234 27.23);
--sidebar-accent-foreground: oklch(0.9889 0.004 286.33);
--sidebar-border: oklch(0.88 0.003 286.33);
--sidebar-ring: oklch(0.6034 0.234 27.23);
}
.dark {
--background: oklch(0.145 0 0);
--foreground: oklch(0.985 0 0);
--card: oklch(0.145 0 0);
--card-foreground: oklch(0.985 0 0);
--popover: oklch(0.145 0 0);
--popover-foreground: oklch(0.985 0 0);
--primary: oklch(0.985 0 0);
--primary-foreground: oklch(0.205 0 0);
--secondary: oklch(0.269 0 0);
--secondary-foreground: oklch(0.985 0 0);
--muted: oklch(0.269 0 0);
--muted-foreground: oklch(0.708 0 0);
--accent: oklch(0.269 0 0);
--accent-foreground: oklch(0.985 0 0);
--destructive: oklch(0.396 0.141 25.723);
--destructive-foreground: oklch(0.637 0.237 25.331);
--border: oklch(0.269 0 0);
--input: oklch(0.269 0 0);
--ring: oklch(0.556 0 0);
--chart-1: oklch(0.488 0.243 264.376);
--chart-2: oklch(0.696 0.17 162.48);
--chart-3: oklch(0.769 0.188 70.08);
--chart-4: oklch(0.627 0.265 303.9);
--chart-5: oklch(0.645 0.246 16.439);
--sidebar: oklch(0.205 0 0);
--sidebar-foreground: oklch(0.985 0 0);
--sidebar-primary: oklch(0.488 0.243 264.376);
--sidebar-primary-foreground: oklch(0.985 0 0);
--sidebar-accent: oklch(0.269 0 0);
--sidebar-accent-foreground: oklch(0.985 0 0);
--sidebar-border: oklch(0.269 0 0);
--sidebar-ring: oklch(0.439 0 0);
--background: oklch(0.18 0.01 286.33);
--foreground: oklch(0.9889 0.004 286.33);
--card: oklch(0.22 0.01 286.33);
--card-foreground: oklch(0.9889 0.004 286.33);
--popover: oklch(0.22 0.01 286.33);
--popover-foreground: oklch(0.9889 0.004 286.33);
--primary: oklch(0.7044 0.184 22.51);
--primary-foreground: oklch(0.18 0.01 286.33);
--secondary: oklch(0.36 0.005 271.31);
--secondary-foreground: oklch(0.9889 0.004 286.33);
--muted: oklch(0.25 0.01 286.33);
--muted-foreground: oklch(0.7 0.01 286.33);
--accent: oklch(0.8034 0.234 27.23);
--accent-foreground: oklch(0.18 0.01 286.33);
--destructive: oklch(0.65 0.18 27.23);
--destructive-foreground: oklch(0.18 0.01 286.33);
--border: oklch(0.25 0.01 286.33);
--input: oklch(0.22 0.01 286.33);
--ring: oklch(0.8034 0.234 27.23);
--chart-1: oklch(0.7044 0.184 22.51);
--chart-2: oklch(0.8034 0.234 27.23);
--chart-3: oklch(0.36 0.005 271.31);
--chart-4: oklch(0.7 0.15 120);
--chart-5: oklch(0.7 0.15 300);
--sidebar: oklch(0.22 0.01 286.33);
--sidebar-foreground: oklch(0.9889 0.004 286.33);
--sidebar-primary: oklch(0.7044 0.184 22.51);
--sidebar-primary-foreground: oklch(0.18 0.01 286.33);
--sidebar-accent: oklch(0.8034 0.234 27.23);
--sidebar-accent-foreground: oklch(0.18 0.01 286.33);
--sidebar-border: oklch(0.25 0.01 286.33);
--sidebar-ring: oklch(0.8034 0.234 27.23);
}
@theme inline {