Skip to content
Snippets Groups Projects
Commit e627e255 authored by Yusuf Akgül's avatar Yusuf Akgül :hatching_chick:
Browse files

init auth fix

parent 6c59f1e2
No related branches found
No related tags found
1 merge request!19Feat.auth fixes
Pipeline #36086 failed
import { prisma } from '@/lib/db' import { authOptions } from '@/lib/auth'
import { compare } from 'bcrypt' import NextAuth from 'next-auth'
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,
}
}
})
],
callbacks: {
session: ({ session, token }) => {
console.log('Session Callback', { session, token })
return {
...session,
user: {
...session.user,
id: token.id,
}
}
},
jwt: ({ token, user }) => {
console.log('JWT Callback', { token, user })
if (user) {
const u = user as unknown as any
return {
...token,
id: u.id,
}
}
return token
}
}
}
const handler = NextAuth(authOptions) const handler = NextAuth(authOptions)
export { handler as GET, handler as POST } export { handler as GET, handler as POST }
import { prisma } from "@/lib/db" import { authOptions } from "@/lib/auth";
import { NextRequest, NextResponse } from "next/server" import { prisma } from "@/lib/db";
import { getServerSession } from "next-auth/next"
import { authOptions } from "../auth/[...nextauth]/route";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import { revalidatePath, revalidateTag } from "next/cache"; import { getServerSession } from "next-auth/next";
import { revalidatePath } from "next/cache";
import { NextRequest, NextResponse } from "next/server";
type post = Prisma.PostUncheckedCreateInput type post = Prisma.PostUncheckedCreateInput
...@@ -22,7 +22,7 @@ export async function POST(req: NextRequest) { ...@@ -22,7 +22,7 @@ export async function POST(req: NextRequest) {
try { try {
await prisma.post.create({ await prisma.post.create({
/* data: data */ /* data: data */
data:{ data: {
content: data.content, content: data.content,
userId: parseInt(userId), userId: parseInt(userId),
published: true published: true
...@@ -45,7 +45,7 @@ export async function GET(req: NextRequest, res: NextResponse) { ...@@ -45,7 +45,7 @@ export async function GET(req: NextRequest, res: NextResponse) {
const data = await req.json() const data = await req.json()
console.log("router data: " + data, "status:") console.log("router data: " + data, "status:")
} catch (error) { } catch (error) {
} }
try { try {
......
import { authOptions } from '@/lib/auth'
import { getServerSession } from 'next-auth/next' import { getServerSession } from 'next-auth/next'
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { authOptions } from './auth/[...nextauth]/route'
export async function GET(request: Request) { export async function GET() {
const session = await getServerSession(authOptions) const session = await getServerSession(authOptions)
if (!session) { if (!session) {
return new NextResponse(JSON.stringify({ error: 'unauthorized' }), { return new NextResponse(JSON.stringify({ error: 'unauthorized' }), { status: 401 })
status: 401
})
} }
console.log('GET API', session)
return NextResponse.json({ authenticated: !!session }) return NextResponse.json({ authenticated: !!session })
} }
\ No newline at end of file
...@@ -16,11 +16,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) ...@@ -16,11 +16,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const existingUser = await prisma.user.findUnique({ const existingUser = await prisma.user.findUnique({
where: { where: {
id : +userId id: +userId
} }
}); });
return res.status(200).json({ ...existingUser}); return res.status(200).json({ ...existingUser });
} catch (error) { } catch (error) {
console.log(error); console.log(error);
return res.status(400).end(); return res.status(400).end();
......
...@@ -15,7 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) ...@@ -15,7 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}); });
return res.status(200).json(users); return res.status(200).json(users);
} catch(error) { } catch (error) {
console.log(error); console.log(error);
return res.status(400).end(); return res.status(400).end();
} }
......
import { compare } from "bcrypt"
import { NextAuthOptions } from "next-auth"
import CredentialsProvider from 'next-auth/providers/credentials'
import { prisma } from "./db"
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,
}
}
})
],
callbacks: {
session: ({ session, token }) => {
console.log('Session Callback', { session, token })
return {
...session,
user: {
...session.user,
id: token.id,
}
}
},
jwt: ({ token, user }) => {
console.log('JWT Callback', { token, user })
if (user) {
const u = user as unknown as any
return {
...token,
id: u.id,
}
}
return token
}
}
}
\ No newline at end of file
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