feat: working login and logged out
This commit is contained in:
+6
-15
@@ -1,8 +1,7 @@
|
||||
import { getCookies, setCookie } from "@std/http/cookie";
|
||||
import { z } from "zod";
|
||||
import { backendSignIn } from "../backend/sign-in.ts";
|
||||
import { SignInForm } from "../components/SignInForm.tsx";
|
||||
import { define } from "../utils.ts";
|
||||
import { define, isLoggedIn, setAuthCookie } from "../utils.ts";
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string(),
|
||||
@@ -11,13 +10,12 @@ const loginSchema = z.object({
|
||||
|
||||
export const handler = define.handlers({
|
||||
GET(ctx) {
|
||||
const cookies = getCookies(ctx.req.headers);
|
||||
const authCookie = cookies["auth"];
|
||||
if (authCookie === "ok") {
|
||||
if (isLoggedIn(ctx.req)) {
|
||||
const url = new URL(ctx.req.url);
|
||||
url.pathname = "/dashboard";
|
||||
return Response.redirect(url, 303);
|
||||
}
|
||||
|
||||
return {
|
||||
data: {
|
||||
email: "",
|
||||
@@ -52,17 +50,10 @@ export const handler = define.handlers({
|
||||
};
|
||||
}
|
||||
|
||||
const url = new URL(ctx.req.url);
|
||||
const headers = new Headers();
|
||||
setCookie(headers, {
|
||||
name: "auth",
|
||||
value: "ok",
|
||||
maxAge: 120,
|
||||
sameSite: "Lax",
|
||||
domain: url.hostname,
|
||||
path: "/",
|
||||
secure: true,
|
||||
});
|
||||
setAuthCookie(ctx.req, headers);
|
||||
|
||||
const url = new URL(ctx.req.url);
|
||||
url.pathname = "/dashboard";
|
||||
headers.set("location", url.toString());
|
||||
return new Response(null, {
|
||||
|
||||
Reference in New Issue
Block a user