boredgame/src/lib/server/api/common/middleware/require-auth.middleware.ts

31 lines
922 B
TypeScript
Raw Normal View History

2024-12-01 23:34:04 +00:00
import { Unauthorized } from '$lib/server/api/common/utils/exceptions';
import type { Sessions, Users } from '$lib/server/api/databases/postgres/tables';
import type { MiddlewareHandler } from 'hono';
import { createMiddleware } from 'hono/factory';
2024-09-04 23:04:41 +00:00
export const requireFullAuth: MiddlewareHandler<{
Variables: {
session: Sessions;
user: Users;
};
}> = createMiddleware(async (c, next) => {
const session = c.var.session;
if (!session || (session?.twoFactorAuthEnabled && !session?.twoFactorVerified)) {
throw Unauthorized('You must be logged in to access this resource');
}
return next();
});
export const requireTempAuth: MiddlewareHandler<{
2024-12-01 23:34:04 +00:00
Variables: {
session: Sessions;
user: Users;
};
2024-09-04 23:04:41 +00:00
}> = createMiddleware(async (c, next) => {
2024-12-01 23:34:04 +00:00
const session = c.var.session;
if (!session) {
throw Unauthorized('You must be logged in to access this resource');
}
return next();
});