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