121 lines
3.4 KiB
TypeScript
121 lines
3.4 KiB
TypeScript
// middleware/auth.global.ts
|
|
import { useUserInfo } from "~/composables/useUserInfo";
|
|
|
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
|
// Skip di server side
|
|
if (process.server) return;
|
|
|
|
const userInfo = useUserInfo();
|
|
|
|
console.log("Global Auth Middleware - Route:", to.fullPath);
|
|
console.log("Global Auth Middleware - Meta auth:", to.meta.auth);
|
|
console.log("Global Auth Middleware - isLoading:", userInfo.isLoading.value);
|
|
console.log(
|
|
"Global Auth Middleware - isAuthenticated:",
|
|
userInfo.isAuthenticated.value
|
|
);
|
|
|
|
// **PERBAIKAN: Enhanced Loading Wait dengan timeout yang masuk akal**
|
|
const maxWaitTime = 10 * 1000; // 10 detik (lebih masuk akal dari 5 menit)
|
|
const startTime = Date.now();
|
|
|
|
while (userInfo.isLoading.value) {
|
|
const elapsedTime = Date.now() - startTime;
|
|
if (elapsedTime >= maxWaitTime) {
|
|
// console.warn(
|
|
// "Global Auth Middleware: Waiting for user info timed out after 10 seconds"
|
|
// );
|
|
break;
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
}
|
|
|
|
// console.log(
|
|
// "Global Auth Middleware - isAuthenticated:",
|
|
// userInfo.isAuthenticated.value
|
|
// );
|
|
|
|
// **PERBAIKAN: Handle Query Parameters**
|
|
let reason = to.query.reason as string;
|
|
const shouldContinue = to.query.continue === "true";
|
|
console.log("Global Auth Middleware - reson:", reason);
|
|
// === LOGIC UNTUK ROUTE PUBLIK ===
|
|
if (to.meta.auth === false) {
|
|
//console.log("Global Auth Middleware - Public route, allowing access");
|
|
return;
|
|
}
|
|
|
|
// === LOGIC UNTUK ROUTE GUEST (LOGIN PAGE) ===
|
|
console.log("Auth middleware logic: to.meta.auth =", to.meta.auth);
|
|
if (to.meta.auth === "guest") {
|
|
console.log(
|
|
"Guest middleware logic: isAuthenticated =",
|
|
userInfo.isAuthenticated.value
|
|
);
|
|
|
|
// Add check for reason=auth_required to treat as idle for continue panel
|
|
if (
|
|
reason === "auth_required" &&
|
|
!userInfo.isAuthenticated.value &&
|
|
!(
|
|
to.path === "/auth/login" &&
|
|
to.query.reason === "auth_required" &&
|
|
to.query.continue === "true"
|
|
)
|
|
) {
|
|
return navigateTo({
|
|
path: "/auth/login",
|
|
query: {
|
|
...to.query,
|
|
reason: "auth_required",
|
|
continue: "true"
|
|
}
|
|
});
|
|
}
|
|
}
|
|
// === LOGIC UNTUK ROUTE YANG MEMERLUKAN AUTH ===
|
|
|
|
if (to.meta.auth === "required") {
|
|
if (!userInfo.isAuthenticated.value) {
|
|
//console.log("Global Auth Middleware: redirecting to login");
|
|
return navigateTo({
|
|
path: "/auth/login/",
|
|
query: {
|
|
returnUrl: to.fullPath,
|
|
reason: "auth_required",
|
|
continue: "true"
|
|
}
|
|
});
|
|
}
|
|
// **PERBAIKAN: Session Validity Check**
|
|
try {
|
|
await userInfo.refresh();
|
|
const session = userInfo.data.value;
|
|
if (
|
|
!session ||
|
|
(session.expires && new Date(session.expires) <= new Date())
|
|
) {
|
|
//console.log("Session expired, redirecting to login");
|
|
return navigateTo({
|
|
path: "/auth/login",
|
|
query: {
|
|
returnUrl: to.fullPath,
|
|
reason: "session_expired"
|
|
}
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error("Session check failed:", error);
|
|
return navigateTo({
|
|
path: "/auth/login",
|
|
query: {
|
|
returnUrl: to.fullPath,
|
|
reason: "session_error"
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
//console.log("Global Auth Middleware: access granted");
|
|
});
|