diff --git a/.env.example b/.env.example index 53179cb37..7f47ba24d 100644 --- a/.env.example +++ b/.env.example @@ -22,6 +22,7 @@ REDIS_URL= GITHUB_ID= GITHUB_SECRET= NEXT_PUBLIC_DISCORD_WEBHOOK_URL = +JOB_BOARD_AUTH_SECRET= COHORT3_DISCORD_ACCESS_KEY = diff --git a/src/app/api/admin/services/externalLogin/route.ts b/src/app/api/admin/services/externalLogin/route.ts new file mode 100644 index 000000000..d3f34761b --- /dev/null +++ b/src/app/api/admin/services/externalLogin/route.ts @@ -0,0 +1,60 @@ +import db from '@/db'; +import { NextRequest, NextResponse } from 'next/server'; +import bcrypt from 'bcrypt'; + +export async function POST(req: NextRequest) { + const authKey = req.headers.get('Auth-Key'); + + if (authKey !== process.env.JOB_BOARD_AUTH_SECRET) { + return NextResponse.json({ message: 'Unauthorized' }, { status: 403 }); + } + + try { + const payload = await req.json(); + const { email, password } = payload; + const user = await db.user.findFirst({ + where: { + email, + }, + select: { + id: true, + email: true, + name: true, + password: true, + }, + }); + if (!user) { + return NextResponse.json({ message: 'User not found' }, { status: 404 }); + } + + if ( + user && + user.password && + (await bcrypt.compare(password, user.password)) + ) { + const courses = await db.course.findMany({ + where: { + purchasedBy: { + some: { + user: { + email, + }, + }, + }, + }, + }); + return NextResponse.json({ + message: 'User found', + data: { + user, + courses, + }, + }); + } + } catch (error) { + return NextResponse.json( + { message: 'Error fetching user' }, + { status: 500 }, + ); + } +}