Personal no-bs blogging platform.
Something went wrong. Try again.
661 B · 16 lines
TypeScript
at master
1234567891011121314151617import { getCookie } from "hono/cookie";import { createMiddleware } from "hono/factory";
/** * Middleware that redirects if (or not) authenticated. * @param target Where to redirect. "/(whatever)" * @param authenticated Redirect if authenticated, or if not authenticated? */export const redirect_if_auth = (target: string, authenticated: boolean) => { return createMiddleware(async (c, next) => { const session = getCookie(c, "session"); if (session && authenticated) return c.redirect(target); else if ((session && !authenticated) || (!session && !authenticated)) return c.redirect("/login"); await next(); });}