Skip to content

Commit

Permalink
move auth check to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Zuniga Cuellar committed Oct 5, 2023
1 parent 84f58fa commit b0e4b8f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
50 changes: 50 additions & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { defineMiddleware } from "astro:middleware";
import { supabase } from "../scripts/supabase";
import { cookieOptions } from "../scripts/utils";

const protectedRoutes = ["/dashboard"];
const redirectRoutes = ["/signin", "/register"];

export const onRequest = defineMiddleware(
async ({ locals, url, cookies, redirect }, next) => {
if (protectedRoutes.includes(url.pathname)) {
const accessToken = cookies.get("sb-access-token");
const refreshToken = cookies.get("sb-refresh-token");

if (!accessToken || !refreshToken) {
return redirect("/signin");
}

const { data, error } = await supabase.auth.setSession({
refresh_token: refreshToken.value,
access_token: accessToken.value,
});

if (error) {
return redirect("/signin");
}

locals.email = data.user?.email!;
cookies.set(
"sb-access-token",
data?.session?.access_token!,
cookieOptions,
);
cookies.set(
"sb-refresh-token",
data?.session?.refresh_token!,
cookieOptions,
);
}

if (redirectRoutes.includes(url.pathname)) {
const accessToken = cookies.get("sb-access-token");
const refreshToken = cookies.get("sb-refresh-token");

if (accessToken && refreshToken) {
return redirect("/dashboard");
}
}
return next();
},
);
31 changes: 2 additions & 29 deletions src/pages/dashboard.astro
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
---
import Base from "../layout/Base.astro";
import { supabase } from "../scripts/supabase";
const accessToken = Astro.cookies.get("sb-access-token");
const refreshToken = Astro.cookies.get("sb-refresh-token");
if (!accessToken || !refreshToken) {
return Astro.redirect("/signin");
}
const { data, error } = await supabase.auth.setSession({
refresh_token: refreshToken.value,
access_token: accessToken.value,
});
if (error || !data.user?.email) {
return Astro.redirect("/signin");
}
Astro.cookies.set("sb-access-token", data?.session?.access_token!, {
path: "/",
secure: true,
httpOnly: true,
});
Astro.cookies.set("sb-refresh-token", data?.session?.refresh_token!, {
path: "/",
secure: true,
httpOnly: true,
});
const {email} = Astro.locals;
---

<Base title="Dashboard">
<p>Hi {data.user.email}</p>
<p>Hi {email}</p>
<a href="/api/auth/signout">Sign out</a>
</Base>
7 changes: 0 additions & 7 deletions src/pages/register.astro
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
---
import Base from "../layout/Base.astro";
const accessToken = Astro.cookies.get("sb-access-token");
const refreshToken = Astro.cookies.get("sb-refresh-token");
if (accessToken && refreshToken) {
return Astro.redirect("/dashboard");
}
---

<Base title="Register">
Expand Down
7 changes: 0 additions & 7 deletions src/pages/signin.astro
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
---
import Base from "../layout/Base.astro";
const accessToken = Astro.cookies.get("sb-access-token");
const refreshToken = Astro.cookies.get("sb-refresh-token");
if (accessToken && refreshToken) {
return Astro.redirect("/dashboard");
}
---

<Base title="Sign in">
Expand Down

0 comments on commit b0e4b8f

Please sign in to comment.