Refactor code structure and remove redundant sections for improved readability and maintainability
All checks were successful
Chore App Build, Test, and Push Docker Images / build-and-push (push) Successful in 1m48s
All checks were successful
Chore App Build, Test, and Push Docker Images / build-and-push (push) Successful in 1m48s
This commit is contained in:
@@ -12,6 +12,31 @@ export const isParentAuthenticated = ref(false)
|
||||
export const isParentPersistent = ref(false)
|
||||
export const parentAuthExpiresAt = ref<number | null>(null)
|
||||
|
||||
// --- Background expiry watcher ---
|
||||
// Declared before the init block so startParentExpiryWatcher is safe to call during restore.
|
||||
let expiryWatcherIntervalId: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
function runExpiryCheck() {
|
||||
if (parentAuthExpiresAt.value !== null && Date.now() >= parentAuthExpiresAt.value) {
|
||||
applyParentLoggedOutState()
|
||||
if (typeof window !== 'undefined') {
|
||||
window.location.href = '/child'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function startParentExpiryWatcher() {
|
||||
stopParentExpiryWatcher()
|
||||
expiryWatcherIntervalId = setInterval(runExpiryCheck, 15_000)
|
||||
}
|
||||
|
||||
export function stopParentExpiryWatcher() {
|
||||
if (expiryWatcherIntervalId !== null) {
|
||||
clearInterval(expiryWatcherIntervalId)
|
||||
expiryWatcherIntervalId = null
|
||||
}
|
||||
}
|
||||
|
||||
// Restore persistent parent auth from localStorage on store init
|
||||
if (hasLocalStorage) {
|
||||
try {
|
||||
@@ -38,30 +63,6 @@ export const currentUserId = ref('')
|
||||
export const suppressForceLogout = ref(false)
|
||||
let authSyncInitialized = false
|
||||
|
||||
// --- Background expiry watcher ---
|
||||
let expiryWatcherIntervalId: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
function runExpiryCheck() {
|
||||
if (parentAuthExpiresAt.value !== null && Date.now() >= parentAuthExpiresAt.value) {
|
||||
applyParentLoggedOutState()
|
||||
if (typeof window !== 'undefined') {
|
||||
window.location.href = '/child'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function startParentExpiryWatcher() {
|
||||
stopParentExpiryWatcher()
|
||||
expiryWatcherIntervalId = setInterval(runExpiryCheck, 15_000)
|
||||
}
|
||||
|
||||
export function stopParentExpiryWatcher() {
|
||||
if (expiryWatcherIntervalId !== null) {
|
||||
clearInterval(expiryWatcherIntervalId)
|
||||
expiryWatcherIntervalId = null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicitly checks whether parent auth has expired and clears it if so.
|
||||
* Called by the router guard before allowing /parent routes.
|
||||
@@ -163,10 +164,12 @@ export async function checkAuth() {
|
||||
currentUserId.value = data.id
|
||||
isUserLoggedIn.value = true
|
||||
} else {
|
||||
logoutUser()
|
||||
isUserLoggedIn.value = false
|
||||
currentUserId.value = ''
|
||||
}
|
||||
} catch {
|
||||
logoutUser()
|
||||
isUserLoggedIn.value = false
|
||||
currentUserId.value = ''
|
||||
}
|
||||
isAuthReady.value = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user