Fix duplicate /api/auth/member call and add request deduplication
Remove redundant checkMemberStatus() from coming-soon page since the auth-init plugin already handles it. Add in-flight request deduplication to prevent concurrent calls from any source. Strip debug console.logs.
This commit is contained in:
parent
52bb17e25d
commit
78c592c13a
3 changed files with 22 additions and 40 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
let pendingRequest = null;
|
||||||
|
|
||||||
export const useAuth = () => {
|
export const useAuth = () => {
|
||||||
const memberData = useState("auth.member", () => null);
|
const memberData = useState("auth.member", () => null);
|
||||||
|
|
||||||
|
|
@ -6,29 +8,24 @@ export const useAuth = () => {
|
||||||
const isMember = computed(() => !!memberData.value);
|
const isMember = computed(() => !!memberData.value);
|
||||||
|
|
||||||
const checkMemberStatus = async () => {
|
const checkMemberStatus = async () => {
|
||||||
console.log("🔍 checkMemberStatus called");
|
if (pendingRequest) {
|
||||||
console.log(" - Current memberData:", !!memberData.value);
|
return pendingRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
pendingRequest = (async () => {
|
||||||
try {
|
try {
|
||||||
console.log(" - Making API call to /api/auth/member...");
|
|
||||||
const response = await $fetch("/api/auth/member");
|
const response = await $fetch("/api/auth/member");
|
||||||
console.log(" - API response received:", {
|
|
||||||
email: response.email,
|
|
||||||
id: response.id,
|
|
||||||
});
|
|
||||||
memberData.value = response;
|
memberData.value = response;
|
||||||
console.log(" - ✅ Member authenticated successfully");
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
|
||||||
" - ❌ Failed to fetch member status:",
|
|
||||||
error.statusCode,
|
|
||||||
error.statusMessage,
|
|
||||||
);
|
|
||||||
memberData.value = null;
|
memberData.value = null;
|
||||||
console.log(" - Cleared memberData");
|
|
||||||
return false;
|
return false;
|
||||||
|
} finally {
|
||||||
|
pendingRequest = null;
|
||||||
}
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
return pendingRequest;
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,6 @@ const loginError = ref("");
|
||||||
|
|
||||||
const isFormValid = computed(() => email.value && email.value.includes("@"));
|
const isFormValid = computed(() => email.value && email.value.includes("@"));
|
||||||
|
|
||||||
await checkMemberStatus();
|
|
||||||
|
|
||||||
const handleLogin = async () => {
|
const handleLogin = async () => {
|
||||||
if (isLoggingIn.value) return;
|
if (isLoggingIn.value) return;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,7 @@
|
||||||
export default defineNuxtPlugin(async () => {
|
export default defineNuxtPlugin(async () => {
|
||||||
const { memberData, checkMemberStatus } = useAuth()
|
const { memberData, checkMemberStatus } = useAuth()
|
||||||
|
|
||||||
console.log('🚀 Auth init plugin running on CLIENT')
|
|
||||||
|
|
||||||
// Only initialize if we don't already have member data
|
|
||||||
if (!memberData.value) {
|
if (!memberData.value) {
|
||||||
console.log(' - No member data, checking auth status...')
|
await checkMemberStatus()
|
||||||
|
|
||||||
const isAuthenticated = await checkMemberStatus()
|
|
||||||
|
|
||||||
if (isAuthenticated) {
|
|
||||||
console.log(' - ✅ Authentication successful')
|
|
||||||
} else {
|
|
||||||
console.log(' - ❌ No valid authentication')
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log(' - ✅ Member data already exists:', memberData.value.email)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
Loading…
Add table
Add a link
Reference in a new issue