We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
// page.astro --- import type { APIContext } from "astro"; import type { AuthUser } from "../users"; import { login_path } from "./login_path"; const protected_route = <T extends AuthUser["role"] = "premium">( { locals: { auth }, url, redirect, }: Pick<APIContext, "locals" | "url" | "redirect">, roles: Array<T> = ["premium"] as Array<T> ) => { if (!roles.includes(auth.user.role as T)) { throw Object.assign(redirect(login_path(url), 302), { __internal: true, }); } return { user: auth.user as Extract<AuthUser, { role: T }>, }; }; const { user } = protected_route(Astro) ---
// middleware.ts try { return await next(); } catch (e) { if (e instanceof Response && "__internal" in e && e.__internal === true) { return e; } // report error }
Option 1 Discord message
export class ErrorWithResponse { constructor(message: string, private readonly builder: () => Response | Promise<Response>) { super(message); } render() { return this.builder(); } }
Option 2 Discord message
Example usecase:
Handle auth at page level
The text was updated successfully, but these errors were encountered:
alexanderniebuhr
No branches or pull requests
Option 1: Error details
Option 1 Discord message
Option2: Custom error class
Option 2 Discord message
Example usecase:
Handle auth at page level
The text was updated successfully, but these errors were encountered: