Skip to content
Snippets Groups Projects
Commit 008795d7 authored by Omar Kasbah's avatar Omar Kasbah
Browse files

NextAuth init

parent 910e0e67
No related branches found
No related tags found
1 merge request!13Feat.next auth
Pipeline #35271 failed
import LikeButton from "@/components/like-button";
import PostMessageForm from "@/components/post-messages";
import { prisma } from "@/prisma/db";
import { prisma } from "@/lib/db";
import { Prisma } from "@prisma/client";
type messageType = Prisma.MessageUncheckedCreateInput
......
import { prisma } from '@/lib/db'
import { compare } from 'bcrypt'
import NextAuth, { type NextAuthOptions } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt'
},
providers: [
CredentialsProvider({
name: 'Sign in',
credentials: {
email: {
label: 'Email',
type: 'email',
placeholder: 'hello@example.com'
},
password: { label: 'Password', type: 'password' }
},
async authorize(credentials) {
if (!credentials?.email || !credentials.password) {
return null
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email
}
})
if (!user) {
return null
}
const isPasswordValid = await compare(
credentials.password,
user.password
)
if (!isPasswordValid) {
return null
}
return {
id: user.id + '',
email: user.email,
name: user.name,
randomKey: 'Hey cool'
}
}
})
],
callbacks: {
session: ({ session, token }) => {
console.log('Session Callback', { session, token })
return {
...session,
user: {
...session.user,
id: token.id,
randomKey: token.randomKey
}
}
},
jwt: ({ token, user }) => {
console.log('JWT Callback', { token, user })
if (user) {
const u = user as unknown as any
return {
...token,
id: u.id,
randomKey: u.randomKey
}
}
return token
}
}
}
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
\ No newline at end of file
import { prisma } from "@/prisma/db"
import { prisma } from "@/lib/db"
import { Prisma } from "@prisma/client"
type likeType = Prisma.LikeUncheckedCreateInput
......
import { prisma } from "@/prisma/db"
import { prisma } from "@/lib/db"
import { NextRequest, NextResponse } from "next/server"
export async function POST(req: NextRequest) {
......
import { getServerSession } from 'next-auth/next'
import { NextResponse } from 'next/server'
import { authOptions } from './auth/[...nextauth]/route'
export async function GET(request: Request) {
const session = await getServerSession(authOptions)
if (!session) {
return new NextResponse(JSON.stringify({ error: 'unauthorized' }), {
status: 401
})
}
console.log('GET API', session)
return NextResponse.json({ authenticated: !!session })
}
\ No newline at end of file
File moved
This diff is collapsed.
......@@ -11,40 +11,41 @@ datasource db {
}
model User {
id String @id @default(dbgenerated()) @db.Uuid
userName String @unique
name String?
email String? @unique
emailVerified DateTime?
image String?
Post Post[]
Comment Comment[]
Like Like[]
id String @id @default(dbgenerated()) @db.Uuid
userName String @unique
name String?
email String? @unique
password String
emailVerified DateTime?
image String?
Post Post[]
Comment Comment[]
Like Like[]
}
model Post {
id String @id @default(dbgenerated()) @db.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
content String
likeCount Int? @default(0)
gameId String?
published Boolean @default(false)
userId String @db.Uuid
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Comment Comment[]
Like Like[]
id String @id @default(dbgenerated()) @db.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
content String
likeCount Int? @default(0)
gameId String?
published Boolean @default(false)
userId String @db.Uuid
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Comment Comment[]
Like Like[]
}
model Like {
id String @id @default(dbgenerated()) @db.Uuid
postId String @db.Uuid
userId String @db.Uuid
id String @id @default(dbgenerated()) @db.Uuid
postId String @db.Uuid
userId String @db.Uuid
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model Comment {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment