first user setup and login
This commit is contained in:
7
app/(auth)/dashboard/page.tsx
Normal file
7
app/(auth)/dashboard/page.tsx
Normal file
@@ -0,0 +1,7 @@
|
||||
import React from 'react'
|
||||
|
||||
export default function DashboardPage() {
|
||||
return (
|
||||
<div>DashboardPage</div>
|
||||
)
|
||||
}
|
||||
21
app/(auth)/layout.tsx
Normal file
21
app/(auth)/layout.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
'use client'
|
||||
|
||||
import { SessionProvider, useSession } from 'next-auth/react'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { ReactNode } from 'react'
|
||||
import Navbar from '@/components/Navbar'
|
||||
|
||||
export default function AuthLayout({ children }: { children: ReactNode }) {
|
||||
|
||||
return (
|
||||
<>
|
||||
<SessionProvider>
|
||||
<Navbar />
|
||||
<main className="p-4">
|
||||
{/* Could also add a private header here */}
|
||||
{children}
|
||||
</main>
|
||||
</SessionProvider>
|
||||
</>
|
||||
)
|
||||
}
|
||||
10
app/(public)/layout.tsx
Normal file
10
app/(public)/layout.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import { ReactNode } from 'react'
|
||||
|
||||
export default function PublicLayout({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<main>
|
||||
{/* Public site header if any */}
|
||||
{children}
|
||||
</main>
|
||||
)
|
||||
}
|
||||
10
app/(public)/login/page.tsx
Normal file
10
app/(public)/login/page.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import LoginForm from '@/components/LoginForm';
|
||||
import React, { useState } from 'react'
|
||||
|
||||
export default function LoginPage() {
|
||||
return (
|
||||
<div>
|
||||
<LoginForm />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
76
app/api/auth/[...nextauth]/route.ts
Normal file
76
app/api/auth/[...nextauth]/route.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import NextAuth, { type NextAuthOptions } from 'next-auth'
|
||||
import CredentialsProvider from 'next-auth/providers/credentials'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import bcrypt from 'bcrypt'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export const authOptions: NextAuthOptions = {
|
||||
providers: [
|
||||
CredentialsProvider({
|
||||
name: 'Credentials',
|
||||
credentials: {
|
||||
email: { label: 'Email', type: 'email' },
|
||||
password: { label: 'Password', type: 'password' },
|
||||
},
|
||||
async authorize(credentials) {
|
||||
if (!credentials?.email || !credentials?.password) {
|
||||
console.log('[AUTH] Missing credentials')
|
||||
return null
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email: credentials.email },
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
console.log('[AUTH] User not found')
|
||||
return null
|
||||
}
|
||||
|
||||
if (!user.password) {
|
||||
console.log('[AUTH] User has no password set')
|
||||
return null
|
||||
}
|
||||
|
||||
const isValid = await bcrypt.compare(credentials.password, user.password)
|
||||
if (!isValid) {
|
||||
console.log('[AUTH] Invalid password')
|
||||
return null
|
||||
}
|
||||
|
||||
console.log('[AUTH] Successful login', user.email)
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
role: user.role,
|
||||
}
|
||||
},
|
||||
}),
|
||||
],
|
||||
session: {
|
||||
strategy: 'jwt',
|
||||
},
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (user) {
|
||||
token.id = user.id
|
||||
token.role = user.role
|
||||
}
|
||||
return token
|
||||
},
|
||||
async session({ session, token }) {
|
||||
if (session.user) {
|
||||
session.user.id = token.id as string
|
||||
session.user.role = token.role as "COUPLE" | "PLANNER" | "GUEST"
|
||||
}
|
||||
return session
|
||||
},
|
||||
},
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
}
|
||||
|
||||
const handler = NextAuth(authOptions)
|
||||
export { handler as GET, handler as POST }
|
||||
Reference in New Issue
Block a user