2024-11-08 21:57:13 +00:00
|
|
|
import {Unauthorized} from '$lib/server/api/common/exceptions';
|
|
|
|
|
import type {Sessions} from '$lib/server/api/databases/postgres/tables';
|
|
|
|
|
import type {MiddlewareHandler} from 'hono';
|
|
|
|
|
import {createMiddleware} from 'hono/factory';
|
|
|
|
|
import type {User} from 'lucia';
|
2024-09-04 23:04:41 +00:00
|
|
|
|
|
|
|
|
export const requireAuth: MiddlewareHandler<{
|
|
|
|
|
Variables: {
|
2024-11-08 01:34:13 +00:00
|
|
|
session: Sessions;
|
|
|
|
|
user: User;
|
|
|
|
|
};
|
2024-09-04 23:04:41 +00:00
|
|
|
}> = createMiddleware(async (c, next) => {
|
2024-11-08 01:34:13 +00:00
|
|
|
const user = c.var.user;
|
|
|
|
|
if (!user) throw Unauthorized('You must be logged in to access this resource');
|
|
|
|
|
return next();
|
|
|
|
|
});
|