Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add adminAuth function #329

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ export async function userAuth() {
}
return session;
}

export async function adminAuth() {
const session = await userAuth();
if (!session.user.isAdmin) {
throw new CMError(CMErrorType.Forbidden);
}
return session;
}
2 changes: 2 additions & 0 deletions src/utils/cmerror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export enum CMErrorType {
NoSuchKey,
DuplicateKey,
Unauthorized,
Forbidden,
}

/*
Expand All @@ -33,6 +34,7 @@ const CMERRORTYPE_DEFS = [
[CMErrorType.NoSuchKey, 404, '%s not found', 'Key not found'],
[CMErrorType.DuplicateKey, 409, 'Duplicate %s', 'Duplicate key'],
[CMErrorType.Unauthorized, 401, 'Unauthorized %s', 'Unauthorized'],
[CMErrorType.Forbidden, 403, 'Forbidden %s', 'Forbidden'],
] as const;

// Corresponding response status codes for error types
Expand Down
Loading