@mundogamernetwork/shared-ui 1.16.4 → 1.16.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/composables/useLogout.ts +38 -23
- package/package.json +1 -1
package/composables/useLogout.ts
CHANGED
|
@@ -1,41 +1,56 @@
|
|
|
1
|
-
import { default as AuthService } from "../services/authService";
|
|
2
|
-
|
|
3
1
|
export function useLogout() {
|
|
4
2
|
const authStore = useAuthStore();
|
|
5
3
|
const runtimeConfig = useRuntimeConfig();
|
|
6
4
|
const accountsBaseUrl = runtimeConfig.public.mgSharedUi?.accountsBaseUrl || runtimeConfig.public.accountsBaseUrl;
|
|
5
|
+
// Same host login uses (see MgLoginRegisterMenu's goLogin) — the platform's
|
|
6
|
+
// own API, not accountsBaseUrl. That's where OAuthLogoutController's real
|
|
7
|
+
// /logout route lives.
|
|
8
|
+
const apiBase = (
|
|
9
|
+
(runtimeConfig.public.mgSharedUi?.apiBaseURL as string) ||
|
|
10
|
+
(runtimeConfig.public.apiBaseURL as string) ||
|
|
11
|
+
(import.meta.env.VITE_API_BASE_URL as string) ||
|
|
12
|
+
""
|
|
13
|
+
).replace(/\/api\/v1\/?$/, "");
|
|
7
14
|
|
|
8
15
|
const performLogout = async (redirectTo?: string) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} catch {
|
|
12
|
-
// continue with cleanup even if server call fails
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Clear auth state
|
|
16
|
+
// Clear client-visible auth state immediately so the UI doesn't sit in
|
|
17
|
+
// a signed-in state while the real server-side logout below completes.
|
|
16
18
|
authStore.clearUser();
|
|
17
19
|
authStore.$reset();
|
|
18
|
-
|
|
19
|
-
// Clear cookies client-side
|
|
20
|
-
if (typeof document !== "undefined") {
|
|
21
|
-
const cookiesToClear = ["oauth_token", "browser_id", "mundo_gamer_network_session", "XSRF-TOKEN"];
|
|
22
|
-
cookiesToClear.forEach((name) => {
|
|
23
|
-
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
|
24
|
-
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=${window.location.hostname};`;
|
|
25
|
-
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.${window.location.hostname};`;
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Clear localStorage auth data
|
|
30
20
|
if (typeof localStorage !== "undefined") {
|
|
31
21
|
["accessToken", "userData", "userAbility"].forEach((key) => {
|
|
32
22
|
localStorage.removeItem(key);
|
|
33
23
|
});
|
|
34
24
|
}
|
|
35
25
|
|
|
36
|
-
// Redirect
|
|
37
26
|
const target = redirectTo || `${accountsBaseUrl}/login`;
|
|
38
|
-
|
|
27
|
+
|
|
28
|
+
if (typeof document === "undefined" || !apiBase) {
|
|
29
|
+
if (typeof window !== "undefined") window.location.href = target;
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// The real session is an httpOnly `oauth_token` cookie set by the
|
|
34
|
+
// platform's own API — document.cookie can never read or clear an
|
|
35
|
+
// httpOnly cookie, on any domain. Only a real server response can clear
|
|
36
|
+
// it (a Set-Cookie header the browser actually honors), which means a
|
|
37
|
+
// genuine top-level POST navigation to that API's own /logout route,
|
|
38
|
+
// not an axios/fetch call whose response body a script merely inspects.
|
|
39
|
+
// POST is required (the route isn't registered for GET) and CSRF is
|
|
40
|
+
// already exempted server-side for exactly this cross-origin case.
|
|
41
|
+
const form = document.createElement("form");
|
|
42
|
+
form.method = "POST";
|
|
43
|
+
form.action = `${apiBase}/logout`;
|
|
44
|
+
form.style.display = "none";
|
|
45
|
+
|
|
46
|
+
const redirectField = document.createElement("input");
|
|
47
|
+
redirectField.type = "hidden";
|
|
48
|
+
redirectField.name = "redirect_to";
|
|
49
|
+
redirectField.value = target;
|
|
50
|
+
form.appendChild(redirectField);
|
|
51
|
+
|
|
52
|
+
document.body.appendChild(form);
|
|
53
|
+
form.submit();
|
|
39
54
|
};
|
|
40
55
|
|
|
41
56
|
return { performLogout };
|