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 { 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,
}
}
})
],
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
}
}
}
import { authOptions } from '@/lib/auth'
import NextAuth from 'next-auth'
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
import { prisma } from "@/lib/db"
import { NextRequest, NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "../auth/[...nextauth]/route";
import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/db";
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
......@@ -22,7 +22,7 @@ export async function POST(req: NextRequest) {
try {
await prisma.post.create({
/* data: data */
data:{
data: {
content: data.content,
userId: parseInt(userId),
published: true
......@@ -45,7 +45,7 @@ export async function GET(req: NextRequest, res: NextResponse) {
const data = await req.json()
console.log("router data: " + data, "status:")
} catch (error) {
}
try {
......
import { authOptions } from '@/lib/auth'
import { getServerSession } from 'next-auth/next'
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)
if (!session) {
return new NextResponse(JSON.stringify({ error: 'unauthorized' }), {
status: 401
})
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
......@@ -16,11 +16,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const existingUser = await prisma.user.findUnique({
where: {
id : +userId
id: +userId
}
});
return res.status(200).json({ ...existingUser});
return res.status(200).json({ ...existingUser });
} catch (error) {
console.log(error);
return res.status(400).end();
......
......@@ -15,7 +15,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
return res.status(200).json(users);
} catch(error) {
} catch (error) {
console.log(error);
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