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,13 @@
import { Provider } from 'next-auth/providers/index';
import { getProviders } from 'next-auth/react';
export default async () => {
const providers = await getProviders();
console.log(providers);
return (
<div>
<h1 className="text-5xl">Login</h1>
</div>
);
};

View File

@@ -0,0 +1,12 @@
'use client';
import { SessionProvider } from 'next-auth/react';
import { Session } from 'next-auth';
export const NextAuthSessionProvider = ({
children,
session,
}: {
children: React.ReactNode;
session: Session | null;
}) => <SessionProvider session={session}>{children}</SessionProvider>;

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;

View File

@@ -0,0 +1,6 @@
import NextAuth from 'next-auth';
import { options } from './auth';
const handler = NextAuth(options);
export { handler as GET, handler as POST };

View File

@@ -1,34 +1,40 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
import type { Metadata } from 'next';
import { Geist, Geist_Mono } from 'next/font/google';
import './globals.css';
import { getServerSession } from 'next-auth';
import { NextAuthSessionProvider } from './_components/AuthSessionProvider';
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
variable: '--font-geist-sans',
subsets: ['latin'],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
variable: '--font-geist-mono',
subsets: ['latin'],
});
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
title: 'Create Next App',
description: 'Generated by create next app',
};
export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const session = await getServerSession();
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
</body>
<NextAuthSessionProvider session={session}>
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
</body>
</NextAuthSessionProvider>
</html>
);
}

View File

@@ -1,4 +1,10 @@
'use client';
import { useSession } from 'next-auth/react';
export default function Home() {
const { data: session, status } = useSession();
console.log(session, status);
return (
<div>
<h1 className="text-5xl">Hub</h1>