67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
/**
|
|
* @module auth
|
|
* @description verifies user credentials during the log-in action (i.e. against a database)
|
|
* @exports exports `auth`, `signIn`, `signOut` actions
|
|
*/
|
|
import NextAuth from 'next-auth';
|
|
import { authConfig } from './auth.config';
|
|
import Credentials from 'next-auth/providers/credentials';
|
|
import { z } from 'zod';
|
|
// import bcrypt from 'bcrypt';
|
|
import { User } from '@/app/lib/types/User';
|
|
|
|
const dummyUser:User = {
|
|
id: "1",
|
|
email: "nikola.derezic@gmail.com",
|
|
password: "123456",
|
|
name: "Nikola Derezic"
|
|
};
|
|
|
|
async function getUser(email: string): Promise<User | undefined> {
|
|
// temporary use dummyUser instead of db
|
|
if(email === dummyUser.email) {
|
|
return dummyUser;
|
|
}
|
|
|
|
return undefined;
|
|
|
|
// try {
|
|
// const user = await sql<User>`SELECT * FROM users WHERE email=${email}`;
|
|
// return user.rows[0];
|
|
// } catch (error) {
|
|
// console.error('Failed to fetch user:', error);
|
|
// throw new Error('Failed to fetch user.');
|
|
// }
|
|
}
|
|
|
|
export const { auth, 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) {
|
|
return null;
|
|
}
|
|
|
|
const { email, password } = parsedCredentials.data;
|
|
|
|
const user = await getUser(email);
|
|
|
|
if (!user) return null;
|
|
|
|
// const passwordsMatch = await bcrypt.compare(password, user.password);
|
|
const passwordsMatch = password === user.password;
|
|
|
|
if (!passwordsMatch) return null;
|
|
|
|
return user;
|
|
}
|
|
})
|
|
],
|
|
}); |