Auth-Redirect vor Bootstrap verhindern
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
export default defineNuxtRouteMiddleware(async (to, from) => {
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||||
const auth = useAuthStore()
|
const auth = useAuthStore()
|
||||||
const token = useCookie<string | null>("token", { path: "/" }).value
|
const token = auth.getStoredToken()
|
||||||
|
|
||||||
// DEBUG: Was sieht die Middleware wirklich?
|
// DEBUG: Was sieht die Middleware wirklich?
|
||||||
console.log("🔒 Middleware Check auf:", to.path)
|
console.log("🔒 Middleware Check auf:", to.path)
|
||||||
@@ -13,7 +13,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!auth.user && token && process.client) {
|
if (!auth.user && token) {
|
||||||
console.log("🔄 Auth-Bootstrap aus Cookie")
|
console.log("🔄 Auth-Bootstrap aus Cookie")
|
||||||
await auth.initStore()
|
await auth.initStore()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,18 @@ export const useAuthStore = defineStore("auth", {
|
|||||||
return useCookie<string | null>("token", { path: "/" })
|
return useCookie<string | null>("token", { path: "/" })
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getStoredToken() {
|
||||||
|
const rootToken = this.tokenCookie().value
|
||||||
|
if (rootToken || !process.client) return rootToken
|
||||||
|
|
||||||
|
const tokenCookie = document.cookie
|
||||||
|
.split(";")
|
||||||
|
.map((part) => part.trim())
|
||||||
|
.find((part) => part.startsWith("token="))
|
||||||
|
|
||||||
|
return tokenCookie ? decodeURIComponent(tokenCookie.slice("token=".length)) : null
|
||||||
|
},
|
||||||
|
|
||||||
clearScopedTokenCookies() {
|
clearScopedTokenCookies() {
|
||||||
if (!process.client) return
|
if (!process.client) return
|
||||||
|
|
||||||
@@ -113,7 +125,7 @@ export const useAuthStore = defineStore("auth", {
|
|||||||
scheduleSessionTimers(token?: string | null) {
|
scheduleSessionTimers(token?: string | null) {
|
||||||
if (!process.client) return
|
if (!process.client) return
|
||||||
|
|
||||||
const tokenToUse = token || this.tokenCookie().value
|
const tokenToUse = token || this.getStoredToken()
|
||||||
|
|
||||||
this.clearSessionTimers()
|
this.clearSessionTimers()
|
||||||
this.sessionWarningVisible = false
|
this.sessionWarningVisible = false
|
||||||
@@ -173,7 +185,7 @@ export const useAuthStore = defineStore("auth", {
|
|||||||
console.log("Auth initStore")
|
console.log("Auth initStore")
|
||||||
|
|
||||||
// 1. Check: Haben wir überhaupt ein Token?
|
// 1. Check: Haben wir überhaupt ein Token?
|
||||||
const token = this.tokenCookie().value
|
const token = this.getStoredToken()
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
// Kein Token -> Wir sind fertig, User ist Gast.
|
// Kein Token -> Wir sind fertig, User ist Gast.
|
||||||
@@ -298,7 +310,7 @@ export const useAuthStore = defineStore("auth", {
|
|||||||
const tempStore = useTempStore()
|
const tempStore = useTempStore()
|
||||||
|
|
||||||
// Token aus Argument oder Cookie holen
|
// Token aus Argument oder Cookie holen
|
||||||
const tokenToUse = jwt || this.tokenCookie().value
|
const tokenToUse = jwt || this.getStoredToken()
|
||||||
|
|
||||||
if (!tokenToUse) {
|
if (!tokenToUse) {
|
||||||
const wasSessionExpired = this.sessionExpired
|
const wasSessionExpired = this.sessionExpired
|
||||||
|
|||||||
Reference in New Issue
Block a user