EGamalielGZE
Denoβ€’3y agoβ€’
2 replies
EGamalielGZ

How to avoid the method POST to executed in the URL I am redirected to?

How to avoid the method POST to executed in the URL I am redirected to?

routes/admin/login.tsx
import { HandlerContext, Handlers } from "$fresh/server.ts";
import SessionState from "@/model/session.ts";
import { setCookie } from "https://deno.land/std@0.203.0/http/cookie.ts";
import { ADMIN_ROOT_URL } from "@/utils/config.ts";

export const handler: Handlers<any, SessionState> = {
  async POST(req: Request, _ctx: HandlerContext<any, SessionState>) {
    const headers = new Headers(req.headers);
    setCookie(headers, {
      name: "userSession",
      value: "sample",
      maxAge: 60 * 60 * 24 * 7,
    });
    headers.append("Location", ADMIN_ROOT_URL);

    return new Response(null, {
      status: 307,
      headers,
    });
  },
};

export default function LoginPage() {
  return (
    <div>
      <form method="POST">
        <button type="submit">Iniciar sesion</button>
      </form>
    </div>
  );
}


routes/admin/index.tsx
import { Handlers } from "$fresh/server.ts";
import SessionState from "@/model/session.ts";

export const handler: Handlers<any, SessionState> = {
  GET(_req, ctx) {
    return ctx.render();
  },
};

export default function AdminPage() {
  return (
    <div>
      hello admin
    </div>
  );
}
image.png
Was this page helpful?