feat: working login and logged out
This commit is contained in:
+6
-3
@@ -1,7 +1,10 @@
|
||||
import Header from "../components/Header.tsx";
|
||||
import { define } from "../utils.ts";
|
||||
import { define, isLoggedIn } from "../utils.ts";
|
||||
|
||||
export default define.page(function App(ctx) {
|
||||
const Component = ctx.Component;
|
||||
const loggedIn = isLoggedIn(ctx.req);
|
||||
|
||||
export default define.page(function App({ Component }) {
|
||||
return (
|
||||
<html data-theme="light">
|
||||
<head>
|
||||
@@ -10,7 +13,7 @@ export default define.page(function App({ Component }) {
|
||||
<title>Deno Fresh Dummy</title>
|
||||
</head>
|
||||
<body>
|
||||
<Header />
|
||||
<Header loggedIn={loggedIn} />
|
||||
<Component />
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+21
-3
@@ -1,5 +1,23 @@
|
||||
import { define } from "../utils.ts";
|
||||
import Headline from "../components/Headline.tsx";
|
||||
import { define, isLoggedIn } from "../utils.ts";
|
||||
|
||||
export default define.page(function Dashboard(ctx) {
|
||||
return <h1>Dashboard</h1>;
|
||||
export const handler = define.handlers({
|
||||
GET(ctx) {
|
||||
console.log("ASDF");
|
||||
if (!isLoggedIn(ctx.req)) {
|
||||
const url = new URL(ctx.req.url);
|
||||
url.pathname = "/sign-in";
|
||||
return Response.redirect(url, 303);
|
||||
}
|
||||
|
||||
return { data: {} };
|
||||
},
|
||||
});
|
||||
|
||||
export default define.page<typeof handler>(function Dashboard(_ctx) {
|
||||
return (
|
||||
<div class="container mx-auto p-4">
|
||||
<Headline>Dashboard</Headline>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
+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, {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { define, unsetAuthCookie } from "../utils.ts";
|
||||
|
||||
export const handler = define.handlers({
|
||||
GET(ctx) {
|
||||
const headers = new Headers();
|
||||
unsetAuthCookie(headers);
|
||||
|
||||
const url = new URL(ctx.req.url);
|
||||
url.pathname = "/";
|
||||
headers.set("location", url.toString());
|
||||
return new Response(null, {
|
||||
status: 303,
|
||||
headers,
|
||||
});
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user