import NextAuth, { DefaultSession } from 'next-auth' import GitHub from 'next-auth/providers/github' import Google from 'next-auth/providers/google' import { storeUser } from '@/app/actions' import Credentials from 'next-auth/providers/credentials' import { authConfig } from './auth.config' import { z } from 'zod' import { getStringFromBuffer } from './lib/utils' import { getUser } from './app/xauth/login/actions' // override type definitions for session // declare module 'next-auth' { // interface Session { // user: { // id?: string | null | undefined // } & DefaultSession['user'] // } // } export const { auth, handlers: { GET, POST }, signIn, signOut } = NextAuth({ ...authConfig, providers: [ Credentials({ async authorize(credentials) { const parsedCredentials = z .object({ email: z.string().email(), password: z.string().min(6) }) .safeParse(credentials) if (parsedCredentials.success) { const { email, password } = parsedCredentials.data const user = await getUser(email) if (!user) return null const encoder = new TextEncoder() const saltedPassword = encoder.encode(password + user.salt) const hashedPasswordBuffer = await crypto.subtle.digest( 'SHA-256', saltedPassword ) const hashedPassword = getStringFromBuffer(hashedPasswordBuffer) if (hashedPassword === user.password) { return user } else { return null } } return null } }) ] }) // export const { // handlers: { GET, POST }, // auth // } = NextAuth({ // providers: [ // // GitHub, // Google, // ], // callbacks: { // async jwt({ token, profile }) { // if (profile?.id) { // token.id = String(profile.id) // const user = { // ...token, // ...profile, // id: String(profile.id) // } // await storeUser(user) // } // return token // }, // async session({ session, token }) { // if (token?.id) { // session.user.id = String(token.id) // } // return session // } // // uncomment to require authentication // // authorized({ auth }) { // // return !!auth?.user // // } // }, // pages: { // signIn: '/sign-in' // } // })