mirror of
https://github.com/better-auth/better-auth.git
synced 2026-07-31 20:05:38 -05:00
18 lines
447 B
TypeScript
18 lines
447 B
TypeScript
import { auth } from "@/auth";
|
|
import { defineMiddleware } from "astro:middleware";
|
|
|
|
// `context` and `next` are automatically typed
|
|
export const onRequest = defineMiddleware(async (context, next) => {
|
|
const isAuthed = await auth.api
|
|
.getSession({
|
|
headers: context.request.headers,
|
|
})
|
|
.catch((e) => {
|
|
return null;
|
|
});
|
|
if (context.url.pathname === "/dashboard" && !isAuthed) {
|
|
return context.redirect("/");
|
|
}
|
|
return next();
|
|
});
|