Skip to content

Commit

Permalink
Added job-board auth route
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsFlash10 committed Sep 15, 2024
1 parent 164842f commit 6073780
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ REDIS_URL=
GITHUB_ID=
GITHUB_SECRET=
NEXT_PUBLIC_DISCORD_WEBHOOK_URL =
JOB_BOARD_AUTH_SECRET=


COHORT3_DISCORD_ACCESS_KEY =
Expand Down
60 changes: 60 additions & 0 deletions src/app/api/admin/services/externalLogin/route.ts
Original file line number Diff line number Diff line change
@@ -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 },
);
}
}

0 comments on commit 6073780

Please sign in to comment.