Skip to content

Commit

Permalink
tweak admin prop on session
Browse files Browse the repository at this point in the history
  • Loading branch information
js0mmer committed Oct 23, 2024
1 parent 0515a4e commit a5e4250
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ router.get('/google/callback', function (req, res) {
// check if user is an admin
const allowedUsers = JSON.parse(process.env.ADMIN_EMAILS ?? '[]');
if (allowedUsers.includes(user.email)) {
req.session.passport!.admin = true;
req.session.passport!.isAdmin = true;
}
req.session.returnTo = returnTo;
successLogin(req, res);
Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const reviewsRouter = router({
* Delete a review (user can delete their own or admin can delete any through reports)
*/
delete: userProcedure.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => {
if (ctx.session.passport!.admin || (await userWroteReview(ctx.session.passport!.user.id, input.id))) {
if (ctx.session.passport!.isAdmin || (await userWroteReview(ctx.session.passport!.user.id, input.id))) {
await Review.deleteOne({ _id: input.id });
// delete all votes and reports associated with review
await Vote.deleteMany({ reviewID: input.id });
Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const usersRouter = router({
if (!ctx.session?.passport) {
return { admin: false };
} else {
return { admin: ctx.session.passport.admin as boolean };
return { admin: ctx.session.passport.isAdmin as boolean };
}
}),
});
Expand Down
2 changes: 1 addition & 1 deletion api/src/helpers/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const router = trpc.router;
export const publicProcedure = trpc.procedure;

export const adminProcedure = publicProcedure.use(async (opts) => {
if (!opts.ctx.session.passport?.admin) throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Not an admin' });
if (!opts.ctx.session.passport?.isAdmin) throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Not an admin' });

return opts.next(opts);
});
Expand Down
5 changes: 1 addition & 4 deletions api/src/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ declare module 'express-session' {
}

export interface PassportData {
/**
* True if is validated as an admin
*/
admin: boolean;
isAdmin: boolean;
user: User;
}
}
Expand Down

0 comments on commit a5e4250

Please sign in to comment.