Minor tidy up
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "bgApp",
|
||||
"name": "bgapp",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "src/index.ts",
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import {BunRequest as Request} from "bun";
|
||||
import {orm} from "../orm/orm.ts";
|
||||
import jwt from "jsonwebtoken";
|
||||
import {UnwrappedRequest} from "../utilities/guard";
|
||||
|
||||
@@ -19,7 +19,7 @@ const server = Bun.serve({
|
||||
},
|
||||
|
||||
// (optional) fallback for unmatched routes:
|
||||
fetch(request: Request): Response {
|
||||
fetch(): Response {
|
||||
return Response.json({message: "Not found"}, {status: 404});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {beforeAll, afterAll} from 'bun:test';
|
||||
import {beforeAll} from 'bun:test';
|
||||
import Bun from 'bun';
|
||||
import {sql} from "bun";
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import {BunRequest as Request} from 'bun';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import {Claims} from "../orm/orm.ts";
|
||||
import {ErrorResponse} from "./responseHelper";
|
||||
import {UnauthorizedError} from "./errors";
|
||||
import {Claims} from "../orm/claims";
|
||||
|
||||
export function guardRedirect(method: Function, redirectMethod: Function, guardedClaims: string[] | undefined = undefined) {
|
||||
try {
|
||||
@@ -15,13 +17,12 @@ export function guard(method: Function, guardedClaims: string[] | undefined = un
|
||||
const authHeader: string | null = request.headers.get('Authorization')?.replace(/^Bearer /, '') as string ?? null;
|
||||
try {
|
||||
const userClaims: Claims = jwt.verify(authHeader as string, process.env.JWT_SECRET_KEY as string) as Claims;
|
||||
console.log('Claims?', guardedClaims !== undefined, !userClaims.claims.some(x => guardedClaims?.includes(x)))
|
||||
if (guardedClaims !== undefined && !userClaims.claims.some(x => guardedClaims.includes(x))) {
|
||||
throw new Error('Unauthorized');
|
||||
if (guardedClaims !== undefined && !userClaims.claims.some((x: string): boolean => guardedClaims.includes(x))) {
|
||||
throw new UnauthorizedError('Unauthorized');
|
||||
}
|
||||
return method(await unwrap(request, userClaims));
|
||||
} catch (e) {
|
||||
return Response.json({message: 'Authentication failed.'}, {status: 401})
|
||||
} catch (error: any) {
|
||||
return new ErrorResponse(error as Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user