implemented inital nextAuth logic

This commit is contained in:
PxlLoewe
2025-01-18 23:19:16 +01:00
parent 2f8424e56c
commit 58277ba819
21 changed files with 295 additions and 172 deletions

View File

@@ -0,0 +1,53 @@
import NextAuth, { AuthOptions } from 'next-auth';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import Credentials from 'next-auth/providers/credentials';
import { prisma } from '@repo/db';
import bcrypt from 'bcryptjs';
export const options = {
providers: [
Credentials({
credentials: {
email: { label: 'Email', type: 'email', placeholder: 'E-Mail' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials, req) {
try {
if (!credentials) throw new Error('No credentials provided');
const user = await prisma.user.findFirstOrThrow({
where: { email: credentials.email },
});
if (bcrypt.compareSync(credentials.password, user.password)) {
return user;
}
return null;
} catch (error) {
return null;
}
},
}),
],
secret: process.env.SECRET,
session: {
strategy: 'jwt',
maxAge: 30 * 24 * 60 * 60,
},
adapter: PrismaAdapter(prisma),
events: {
async signIn(message) {
console.log('Signed in!', { message });
},
async signOut(message) {
console.log('Signed out!', { message });
},
async createUser(message) {
console.log('User created!', { message });
},
},
pages: {
signIn: '/login',
signOut: '/logout',
error: '/authError',
newUser: '/register',
},
} satisfies AuthOptions;