Guest User

auth.config.ts

a guest
Mar 29th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.86 KB | Source Code | 0 0
  1. import bcrypt from "bcryptjs"
  2. import type { NextAuthConfig } from "next-auth"
  3. import Credentials from "next-auth/providers/credentials"
  4.  
  5. import { getUserByEmail } from "@/lib/api/services/user"
  6. import { LoginSchema } from "@/lib/schemas/auth"
  7.  
  8. export default {
  9.   providers: [
  10.     Credentials({
  11.       async authorize(credentials) {
  12.         const validatedFields = LoginSchema.safeParse(credentials)
  13.  
  14.         if (validatedFields.success) {
  15.           const { email, password } = validatedFields.data
  16.  
  17.           const user = await getUserByEmail(email)
  18.           if (!user || !user.password) return null
  19.  
  20.           const passwordsMatch = await bcrypt.compare(password, user.password)
  21.  
  22.           if (passwordsMatch) return user
  23.         }
  24.  
  25.         return null
  26.       },
  27.     }),
  28.   ].filter(Boolean) as NextAuthConfig["providers"],
  29. } satisfies NextAuthConfig
  30.  
Advertisement
Add Comment
Please, Sign In to add comment