Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Communicate with middleware in pages via errors #242

Open
OliverSpeir opened this issue Sep 17, 2024 · 0 comments
Open

Communicate with middleware in pages via errors #242

OliverSpeir opened this issue Sep 17, 2024 · 0 comments
Assignees

Comments

@OliverSpeir
Copy link
Collaborator

OliverSpeir commented Sep 17, 2024

Option 1: Error details

// page.astro
---
import type { APIContext } from "astro";
import type { AuthUser } from "../users";
import { login_path } from "./#_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

Option2: Custom error class

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants