feat: add subscription plans. Add admin panel. Add password recovery
This commit is contained in:
39
apps/backend/src/modules/admin/lib/access.ts
Normal file
39
apps/backend/src/modules/admin/lib/access.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { MiddlewareHandler } from 'hono';
|
||||
import type { BackendEnv } from '@/http/env';
|
||||
import { forbiddenProblem } from '@/http/problem-builders';
|
||||
import { problemJson, unauthorizedProblem } from '@/http/problem-details';
|
||||
|
||||
/**
|
||||
* The backoffice is for Gruperly's own staff, listed by email in `ADMIN_EMAILS`
|
||||
* (comma separated, case-insensitive). An empty or missing list means nobody.
|
||||
*/
|
||||
export function isAdminEmail(
|
||||
email: string | null | undefined,
|
||||
env: Record<string, string | undefined> = process.env,
|
||||
): boolean {
|
||||
if (!email) return false;
|
||||
const allowed = (env.ADMIN_EMAILS ?? '')
|
||||
.split(',')
|
||||
.map((entry) => entry.trim().toLowerCase())
|
||||
.filter(Boolean);
|
||||
return allowed.includes(email.trim().toLowerCase());
|
||||
}
|
||||
|
||||
export function adminOnlyMiddleware(
|
||||
env: Record<string, string | undefined> = process.env,
|
||||
): MiddlewareHandler<BackendEnv> {
|
||||
return async (c, next) => {
|
||||
const user = c.get('user');
|
||||
if (!user) return problemJson(c, unauthorizedProblem(c.req.path));
|
||||
if (!isAdminEmail(user.email, env)) {
|
||||
return problemJson(
|
||||
c,
|
||||
forbiddenProblem({
|
||||
detail: 'Solo el equipo de Gruperly puede entrar al backoffice.',
|
||||
code: 'admin_required',
|
||||
}),
|
||||
);
|
||||
}
|
||||
await next();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user