implemented inital nextAuth logic
This commit is contained in:
53
apps/hub/app/api/auth/[...nextauth]/auth.ts
Normal file
53
apps/hub/app/api/auth/[...nextauth]/auth.ts
Normal 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;
|
||||
Reference in New Issue
Block a user