Login watcher

This commit is contained in:
2025-10-06 17:14:54 -04:00
parent a50791e74c
commit 291b6743c5
2 changed files with 20 additions and 3 deletions

View File

@@ -230,4 +230,12 @@ async function handleLogout() {
await $fetch('/api/auth/logout', { method: 'POST' })
window.location.reload()
}
// Watch for authentication changes (e.g., if user is deleted)
watch(() => authData.value?.authenticated, (newAuth, oldAuth) => {
// If user was authenticated but now isn't (and it's not initial load)
if (oldAuth === true && newAuth === false) {
window.location.reload()
}
})
</script>

View File

@@ -1,4 +1,4 @@
import { getAuthCookie } from '~/server/utils/auth'
import { getAuthCookie, clearAuthCookie } from '~/server/utils/auth'
import { getUserByUsername } from '~/server/utils/database'
export default defineEventHandler(async (event) => {
@@ -13,9 +13,18 @@ export default defineEventHandler(async (event) => {
const user = getUserByUsername(username)
// If user doesn't exist (was deleted), clear the auth cookie
if (!user) {
clearAuthCookie(event)
return {
authenticated: false,
isAdmin: false
}
}
return {
authenticated: true,
username: user?.username,
isAdmin: user?.is_admin === 1
username: user.username,
isAdmin: user.is_admin === 1
}
})