Skip to content

Commit 73536ba

Browse files
committed
feat(auth): add frontend auth check for admin dashboard page.
1 parent d8fa48b commit 73536ba

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

backend/src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { client, db } from "./drizzle/db";
1818
import { users } from "./drizzle/schema";
1919
import { eq } from "drizzle-orm";
2020
import { startCronJobs } from "./cron";
21-
import {cruxMembers} from "./middlewares/auth.ts"
21+
import {cruxMembers} from "./middlewares/auth"
2222
import {
2323
fetchContests,
2424
fetchProblems,
@@ -118,10 +118,14 @@ passport.deserializeUser(async (userId: string, done) => {
118118
.then((rows) => rows[0]);
119119

120120
if (user) {
121-
user.isCruxMember = cruxMembers.has(user.id);
121+
const userwithisCruxMemberProperty= {
122+
...user,
123+
isCruxMember: cruxMembers.has(user.cfHandle ?? ''),
124+
};
125+
126+
return done(null, userwithisCruxMemberProperty);
122127
}
123-
124-
done(null, user);
128+
done(null, null);
125129
} catch (error) {
126130
console.error("Error deserializing user:", error);
127131
done(new Error("Could not deserialize user"), null);

backend/src/middlewares/auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ export function requireAuth(req: Request, res: Response, next: NextFunction) {
1313
next();
1414
}
1515

16-
const cruxMembers = new Set<string>([
16+
export const cruxMembers = new Set<string>([
1717
"cwswastik",
1818
"Rittin_Sehgal",
1919
"manjot1151",
2020
"aten2005",
2121
"Yash_Kejriwal",
2222
"Ellufino",
23+
"chicubed",
2324
]);
2425

2526
export const requireCruxMember: RequestHandler = async (

frontend/src/routes/events/$slug/admin.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { useState, useEffect, useCallback } from 'react';
2-
import { createFileRoute } from '@tanstack/react-router';
2+
import { createFileRoute, useParams, Navigate } from '@tanstack/react-router';
33
import axios from 'axios';
44
import {
55
Trash2, Pencil, Users, UserPlus, Trophy, Mail, CheckCircle, History, RefreshCw
66
} from 'lucide-react';
7-
import { useParams } from '@tanstack/react-router'
87

9-
const emailRegex = /^f\d{8}@hyderabad\.bits-pilani\.ac\.in$/;
8+
import { useAuth } from "../../../context/AuthContext.tsx";
9+
1010
interface GroupMember {
1111
email: string;
1212
participantId: number;
@@ -25,13 +25,29 @@ interface PastContest {
2525
duration: string;
2626
}
2727

28+
const emailRegex = /^f\d{8}@hyderabad\.bits-pilani\.ac\.in$/;
2829
const scrollbar = "[&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar-thumb]:bg-slate-700 [&::-webkit-scrollbar-thumb]:rounded-full";
2930

3031
export const Route = createFileRoute('/events/$slug/admin')({
3132
component: AdminEventsComponent,
3233
});
3334

3435
function AdminEventsComponent() {
36+
const { user, loading } = useAuth();
37+
38+
if (loading) {
39+
return (
40+
<div className="min-h-screen flex items-center justify-center bg-slate-900 text-slate-300">
41+
Loading admin dashboard...
42+
</div>
43+
);
44+
}
45+
46+
// Kick out non crux guys
47+
if (!user || !user.isCruxMember) {
48+
return <Navigate to="/events" replace />;
49+
}
50+
3551
const [contestId, setContestId] = useState('');
3652
const [isContestSaved, setIsContestSaved] = useState(false);
3753

0 commit comments

Comments
 (0)