diff --git a/astro.config.mjs b/astro.config.mjs index ffde277..41abc79 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -3,6 +3,7 @@ import { defineConfig } from "astro/config"; import react from "@astrojs/react"; import node from "@astrojs/node"; +// https://astro.build/config export default defineConfig({ integrations: [react()], output: "server", diff --git a/biome.json b/biome.json index f8702e4..3867749 100644 --- a/biome.json +++ b/biome.json @@ -1,5 +1,5 @@ { - "$schema": "https://biomejs.dev/schemas/1.5.3/schema.json", + "$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", "organizeImports": { "enabled": true }, diff --git a/bun.lockb b/bun.lockb index 5491c44..0661eb3 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 77a3951..afc3877 100644 --- a/package.json +++ b/package.json @@ -4,24 +4,24 @@ "version": "0.0.1", "scripts": { "dev": "bunx --bun astro dev", - "start": "bunx --bun astro dev", "build": "rm -rf dist && bunx --bun astro check && bunx --bun astro build", "preview": "bunx --bun astro preview", "astro": "astro" }, "dependencies": { "@astrojs/check": "^0.7.0", - "@astrojs/node": "^8.2.5", - "@astrojs/react": "^3.4.0", - "@types/react": "^18.3.2", + "@astrojs/node": "^8.3.2", + "@astrojs/react": "^3.6.0", + "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", - "astro": "^4.9.1", + "astro": "^4.11.3", "bootstrap": "^5.3.3", "react": "^18.3.1", "react-dom": "^18.3.1", - "typescript": "^5.3.3" + "react-icons": "^5.2.1", + "typescript": "^5.5.2" }, "devDependencies": { - "@biomejs/biome": "1.7.3" + "@biomejs/biome": "1.8.3" } } diff --git a/src/components/Header.astro b/src/components/Header.astro new file mode 100644 index 0000000..91bf02b --- /dev/null +++ b/src/components/Header.astro @@ -0,0 +1,3 @@ +
Movies
+
Settings
+
Logout
diff --git a/src/components/forms/LoginFormData.tsx b/src/components/forms/LoginFormData.tsx index c72bcd5..13a6224 100644 --- a/src/components/forms/LoginFormData.tsx +++ b/src/components/forms/LoginFormData.tsx @@ -2,7 +2,9 @@ import LoginForm, { type LoginFormValue } from "./LoginForm"; export default function LoginFormData() { function handleOnFormSubmit(data: LoginFormValue) { - fetch(`${import.meta.env.PUBLIC_API_URL}/accounts/login`, { + const searchParams = new URLSearchParams(window.location.search); + + fetch("/api/accounts/login", { method: "POST", body: JSON.stringify(data), credentials: "include", @@ -13,7 +15,7 @@ export default function LoginFormData() { }) .then((res) => res.json()) .then((json) => { - window.location.href = "/home"; + window.location.href = searchParams.get("redirect") || "/home"; }) .catch((err) => console.log(err)); } diff --git a/src/layouts/HomeLayout.astro b/src/layouts/HomeLayout.astro index 0bf5bfb..54f5ebc 100644 --- a/src/layouts/HomeLayout.astro +++ b/src/layouts/HomeLayout.astro @@ -1,4 +1,5 @@ --- +import Header from "../components/Header.astro"; import RootLayout from "./RootLayout.astro"; interface Props { @@ -8,13 +9,11 @@ interface Props { const { title } = Astro.props; --- - +
-
Films
-
Compte
-
Deconnexion
+
- +
@@ -22,4 +21,17 @@ const { title } = Astro.props;
- + diff --git a/src/middleware.ts b/src/middleware.ts index 5edb62e..5ee1181 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -2,22 +2,25 @@ import { defineMiddleware, sequence } from "astro:middleware"; const protectedRoutes = defineMiddleware(async (ctx, next) => { const pathname = ctx.url.pathname; - if (!["/", "/register"].includes(pathname)) { - const jwt = ctx.cookies.get("token"); + if ( + !["/", "/register", "/logout"].includes(pathname) && + !pathname.startsWith("/api") + ) { + const jwt = ctx.cookies.get("jwt"); if (!jwt) { - return ctx.redirect("/"); + return ctx.redirect(`/?redirect=${pathname}`); } - const res = await fetch( - `${import.meta.env.PUBLIC_API_URL}/accounts/recovery`, - { - credentials: "include", - headers: ctx.request.headers, + const jwtValue = jwt.value; + const res = await fetch(`${import.meta.env.API_URL}/accounts/recovery`, { + credentials: "include", + headers: { + Authorization: `Bearer ${jwtValue}`, }, - ); + }); - if (res.status !== 201) { - return ctx.redirect("/"); + if (res.status !== 200) { + return ctx.redirect(`/?redirect=${pathname}`); } ctx.locals.account = JSON.parse(await res.text()).account; diff --git a/src/pages/api/accounts/login.ts b/src/pages/api/accounts/login.ts new file mode 100644 index 0000000..9917114 --- /dev/null +++ b/src/pages/api/accounts/login.ts @@ -0,0 +1,28 @@ +import type { APIRoute } from "astro"; + +export const POST: APIRoute = async ({ request, cookies }) => { + const reqBody = await request.text(); + if (!reqBody) throw new Error("body not found"); + + const res = await fetch(`${import.meta.env.API_URL}/accounts/login`, { + method: "POST", + body: reqBody, + headers: { + "Content-Type": "application/json", + }, + }); + + const resBody = await res.text(); + const resBodyJson = JSON.parse(resBody); + + if (res.status === 201 && resBodyJson?.jwt) { + cookies.set("jwt", resBodyJson.jwt, { + secure: true, + httpOnly: true, + path: "/", + sameSite: "strict", + }); + } + + return new Response(resBody, { status: res.status }); +}; diff --git a/src/pages/home/index.astro b/src/pages/home/index.astro index f38a8aa..3163211 100644 --- a/src/pages/home/index.astro +++ b/src/pages/home/index.astro @@ -4,6 +4,6 @@ import HomeLayout from "../../layouts/HomeLayout.astro"; const account = Astro.locals.account; --- - + Hello {account?.username} diff --git a/src/pages/home/settings/account.astro b/src/pages/home/settings/account.astro new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/home/settings/accounts.astro b/src/pages/home/settings/accounts.astro new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/home/settings/index.astro b/src/pages/home/settings/index.astro new file mode 100644 index 0000000..2925f80 --- /dev/null +++ b/src/pages/home/settings/index.astro @@ -0,0 +1,34 @@ +--- +import { + MdAccountBox, + MdAdminPanelSettings, + MdOutlineLocalMovies, +} from "react-icons/md"; + +import HomeLayout from "../../../layouts/HomeLayout.astro"; +--- + + + + diff --git a/src/pages/home/settings/movies.astro b/src/pages/home/settings/movies.astro new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/logout.astro b/src/pages/logout.astro new file mode 100644 index 0000000..aa5ad8d --- /dev/null +++ b/src/pages/logout.astro @@ -0,0 +1,4 @@ +--- +Astro.cookies.delete("jwt"); +return Astro.redirect("/"); +---