diff options
| author | schererleander <leander@schererleander.de> | 2025-12-26 14:57:24 +0100 |
|---|---|---|
| committer | schererleander <leander@schererleander.de> | 2025-12-26 14:57:24 +0100 |
| commit | 86eaae1876f31ef0674d4dc0848d2368fef58372 (patch) | |
| tree | 1c5937919e9f9a8bd3820c9f972f0d7dce78a55e /src/lib | |
| parent | f5ea42ab5db9c880fbeb35282d6da4b3e006fca5 (diff) | |
refactor(auth): cleanup auth options and remove manual db connect
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/auth.ts | 43 |
1 files changed, 10 insertions, 33 deletions
diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 51c6c91..cad3fed 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,13 +1,10 @@ -import NextAuth, { type NextAuthOptions } from "next-auth" +import { type NextAuthOptions } from "next-auth" import CredentialsProvider from "next-auth/providers/credentials" -import { MongoClient } from "mongodb" import bcrypt from "bcryptjs" import dbConnect from "./mongodb" import User from "@/model/User" import { loginSchema } from "./validation" -const client = new MongoClient(process.env.MONGODB_URI!) - export const authOptions: NextAuthOptions = { providers: [ CredentialsProvider({ @@ -17,30 +14,20 @@ export const authOptions: NextAuthOptions = { password: { label: "Password", type: "password" } }, async authorize(credentials) { - if (!credentials?.email || !credentials?.password) { - return null - } + if (!credentials?.email || !credentials?.password) return null - // Validate and sanitize with Zod const result = loginSchema.safeParse(credentials) - - if (!result.success) { - return null - } + if (!result.success) return null const { email, password } = result.data await dbConnect() const user = await User.findOne({ email }) - if (!user) { - return null - } + if (!user) return null const isPasswordValid = await bcrypt.compare(password, user.password) - if (!isPasswordValid) { - return null - } + if (!isPasswordValid) return null return { id: user._id.toString(), @@ -51,21 +38,15 @@ export const authOptions: NextAuthOptions = { } }) ], - session: { - strategy: "jwt" as const - }, + session: { strategy: "jwt" }, callbacks: { - async jwt({ token, user }: { token: any; user: any }) { - if (user) { - token.id = user.id - } + async jwt({ token, user }) { + if (user) token.id = user.id return token }, - async session({ session, token }: { session: any; token: any }) { + async session({ session, token }) { if (token) { session.user.id = token.id as string - - // Fetch latest user data from database to get current profile image await dbConnect() const currentUser = await User.findById(token.id) if (currentUser) { @@ -77,9 +58,5 @@ export const authOptions: NextAuthOptions = { return session }, }, - pages: { - signIn: "/login", - }, + pages: { signIn: "/login" }, } - -export default NextAuth(authOptions)
\ No newline at end of file |
