implemented inital nextAuth logic
This commit is contained in:
13
apps/hub/app/(auth)/login/page.tsx
Normal file
13
apps/hub/app/(auth)/login/page.tsx
Normal 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>
|
||||
);
|
||||
};
|
||||
12
apps/hub/app/_components/AuthSessionProvider.tsx
Normal file
12
apps/hub/app/_components/AuthSessionProvider.tsx
Normal 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>;
|
||||
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;
|
||||
6
apps/hub/app/api/auth/[...nextauth]/route.ts
Normal file
6
apps/hub/app/api/auth/[...nextauth]/route.ts
Normal 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 };
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user