EGamalielGZ
EGamalielGZ•15mo ago

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>
);
}
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>
);
}
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>
);
}
No description
2 Replies
Esente
Esente•15mo ago
307 status code will retain the request method. Try to use 303 or something similar.
EGamalielGZ
EGamalielGZOP•15mo ago
Thanks, with that I'd solved my problem 😄

Did you find this page helpful?