29 lines
669 B
TypeScript
29 lines
669 B
TypeScript
import { Middleware } from "@lib/router.ts";
|
|
import admin from "@lib/admin.ts";
|
|
|
|
const LOGIN_PATH = "/login";
|
|
|
|
const authMiddleware: Middleware = async (c, next) => {
|
|
const token = c.cookies.get("token");
|
|
const isValid = token
|
|
.map((token) => admin.sessions.verifyToken(token))
|
|
.toBoolean();
|
|
const path = c.path;
|
|
|
|
if (path.startsWith("/public")) {
|
|
await next();
|
|
} else {
|
|
if (path !== LOGIN_PATH && !isValid) {
|
|
return c.redirect("/login");
|
|
}
|
|
|
|
if (path === LOGIN_PATH && isValid) {
|
|
return c.redirect("");
|
|
}
|
|
|
|
await next();
|
|
}
|
|
};
|
|
|
|
export default authMiddleware;
|