update Keycloak logout & fullLogout
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@ export default defineNuxtRouteMiddleware(async(to) => {
|
||||
|
||||
try {
|
||||
const sess : any = await useSessionLogin('getSess');
|
||||
// console.log('session data:', sess);
|
||||
console.log('session data:', sess);
|
||||
// console.log('route auth requirement:', timestampToDate(sess?.expires_in?.timestamps, 'id-ID'));
|
||||
// const timeMilis= sess?.expires_in?.timestamps*1000
|
||||
// console.log(timeMilis)
|
||||
@@ -1,34 +0,0 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
// const auth = useAuthStore()
|
||||
// const requiredPermission = to.meta.permission
|
||||
// console.log('requiredPermission', auth)
|
||||
// const userRole = auth.user?.role
|
||||
|
||||
// if (!auth.user) {
|
||||
// return navigateTo('/login')
|
||||
// }
|
||||
|
||||
// if (requiredPermission && !auth.hasPermission(requiredPermission)) {
|
||||
// return navigateTo('/unauthorized')
|
||||
// }
|
||||
|
||||
// // Kalau kamu mau batasi halaman berdasar role:
|
||||
// const allowedRoles = to.meta.roles
|
||||
// if (allowedRoles && !allowedRoles.includes(userRole)) {
|
||||
// return navigateTo('/unauthorized')
|
||||
// }
|
||||
const { status, data: session } = useAuth();
|
||||
if (status.value === "unauthenticated") {
|
||||
console.log("status:", status.value);
|
||||
} else {
|
||||
console.log("status:", status.value);
|
||||
}
|
||||
console.log('masuk middeleware auth-menu')
|
||||
|
||||
|
||||
console.log('route:', to.fullPath)
|
||||
|
||||
|
||||
return navigateTo('/sample-page-copy')
|
||||
})
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
// 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");
|
||||
});
|
||||
Reference in New Issue
Block a user