auth.config.ts132 lines, 3.2 KB
| 1 | import { DefaultSession, NextAuthConfig } from "next-auth"; |
| 2 | import GitHub from "next-auth/providers/github"; |
| 3 | import Google from "next-auth/providers/google"; |
| 4 | import CredentialsProvider from "next-auth/providers/credentials"; |
| 5 | import { z } from "zod"; |
| 6 | import { eq } from "drizzle-orm"; |
| 7 | import { users } from "./db/schema"; |
| 8 | import bcrypt from "bcryptjs"; |
| 9 | import { JWT } from "next-auth/jwt"; |
| 10 | import { db } from "./db"; |
| 11 | |
| 12 | // extend the types to include role |
| 13 | declare module "next-auth" { |
| 14 | interface User { |
| 15 | role?: "user" | "admin"; |
| 16 | } |
| 17 | |
| 18 | interface Session { |
| 19 | user: { |
| 20 | id: string; |
| 21 | role?: "user" | "admin"; |
| 22 | } & DefaultSession["user"]; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | declare module "next-auth/jwt" { |
| 27 | interface JWT { |
| 28 | role?: "user" | "admin"; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | export const authConfig: NextAuthConfig = { |
| 33 | secret: process.env.AUTH_SECRET, |
| 34 | pages: { |
| 35 | signIn: "/login", |
| 36 | signOut: "/logout", |
| 37 | error: "/login", |
| 38 | verifyRequest: "/verify-request", |
| 39 | newUser: "/register", |
| 40 | }, |
| 41 | callbacks: { |
| 42 | authorized({ auth, request: { nextUrl } }) { |
| 43 | const isLoggedIn = !!auth?.user; |
| 44 | const isOnDashboard = nextUrl.pathname.startsWith("/dashboard"); |
| 45 | const isOnAdmin = nextUrl.pathname.startsWith("/admin"); |
| 46 | |
| 47 | if (isOnAdmin) { |
| 48 | return isLoggedIn && auth?.user?.role === "admin"; |
| 49 | } else if (isOnDashboard) { |
| 50 | if (isLoggedIn) return true; |
| 51 | return false; |
| 52 | } else if (isLoggedIn) { |
| 53 | return true; |
| 54 | } |
| 55 | return true; |
| 56 | }, |
| 57 | jwt: async ({ token, user }) => { |
| 58 | if (user) { |
| 59 | token.id = user.id; |
| 60 | token.email = user.email; |
| 61 | token.name = user.name; |
| 62 | token.picture = user.image; |
| 63 | token.role = user.role; |
| 64 | } |
| 65 | return token; |
| 66 | }, |
| 67 | session: async ({ session, token }) => { |
| 68 | if (token) { |
| 69 | session.user.id = token.id as string; |
| 70 | session.user.role = token.role; |
| 71 | } |
| 72 | return session; |
| 73 | }, |
| 74 | }, |
| 75 | |
| 76 | providers: [ |
| 77 | GitHub({ |
| 78 | clientId: process.env.GITHUB_CLIENT_ID, |
| 79 | clientSecret: process.env.GITHUB_CLIENT_SECRET, |
| 80 | }), |
| 81 | Google({ |
| 82 | clientId: process.env.GOOGLE_CLIENT_ID, |
| 83 | clientSecret: process.env.GOOGLE_CLIENT_SECRET, |
| 84 | }), |
| 85 | CredentialsProvider({ |
| 86 | name: "Credentials", |
| 87 | credentials: { |
| 88 | email: { |
| 89 | label: "Email", |
| 90 | type: "email", |
| 91 | placeholder: "example@exmple.com", |
| 92 | }, |
| 93 | password: { label: "Password", type: "password" }, |
| 94 | }, |
| 95 | async authorize(credentials) { |
| 96 | const parsedCredentials = z |
| 97 | .object({ |
| 98 | email: z.string().email(), |
| 99 | password: z.string().min(6), |
| 100 | }) |
| 101 | .safeParse(credentials); |
| 102 | |
| 103 | if (!parsedCredentials.success) return null; |
| 104 | |
| 105 | const { email, password } = parsedCredentials.data; |
| 106 | |
| 107 | const [user] = await db |
| 108 | .select() |
| 109 | .from(users) |
| 110 | .where(eq(users.email, email.toLowerCase())); |
| 111 | |
| 112 | if (!user) return null; |
| 113 | |
| 114 | //check if password is correct |
| 115 | const passwordsMatch = await bcrypt.compare( |
| 116 | password, |
| 117 | user.password || "" |
| 118 | ); |
| 119 | |
| 120 | if (!passwordsMatch) return null; |
| 121 | |
| 122 | return { |
| 123 | id: user.id, |
| 124 | email: user.email, |
| 125 | name: user.name, |
| 126 | image: user.image, |
| 127 | role: user.role, |
| 128 | }; |
| 129 | }, |
| 130 | }), |
| 131 | ], |
| 132 | }; |