import { 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(); }); }