|
| 1 | +import { useStore } from "@/store/main"; |
1 | 2 | import { User } from "@/structures/User";
|
2 |
| -import { log } from "@/Logger"; |
3 | 3 |
|
4 |
| -export function useAuth() { |
5 |
| - function prompt(provider: User.Connection.Platform, token?: string | null): Promise<void> { |
6 |
| - const w = window.open( |
7 |
| - `${import.meta.env.VITE_APP_API_REST}/auth?platform=${provider.toLowerCase()}` + |
8 |
| - (token ? `&token=${token}` : ""), |
9 |
| - "seventv-oauth2", |
10 |
| - "_blank, width=850, height=650, menubar=no, location=no", |
11 |
| - ); |
| 4 | +let authWindow = null as WindowProxy | null; |
| 5 | + |
| 6 | +function popup(route: string): Promise<void> { |
| 7 | + if (authWindow && !authWindow.closed) { |
| 8 | + authWindow.close(); |
| 9 | + } |
12 | 10 |
|
13 |
| - return new Promise((resolve) => { |
14 |
| - const i = setInterval(async () => { |
15 |
| - if (!w?.closed) { |
16 |
| - return; |
17 |
| - } |
| 11 | + const popup = window.open( |
| 12 | + `${import.meta.env.VITE_APP_API_REST}/${route}`, |
| 13 | + "seventv-oauth2", |
| 14 | + "_blank, width=850, height=650, menubar=no, location=no", |
| 15 | + ); |
| 16 | + |
| 17 | + authWindow = popup; |
| 18 | + |
| 19 | + if (!popup) { |
| 20 | + return Promise.reject("Failed to open window"); |
| 21 | + } |
18 | 22 |
|
19 |
| - clearInterval(i); |
20 |
| - resolve(); |
21 |
| - }, 100); |
22 |
| - }); |
| 23 | + return new Promise((resolve) => { |
| 24 | + const i = setInterval(async () => { |
| 25 | + if (!popup.closed) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + clearInterval(i); |
| 30 | + resolve(); |
| 31 | + }, 100); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +export function useAuth() { |
| 36 | + function prompt(provider: User.Connection.Platform, isLink: boolean): Promise<void> { |
| 37 | + const store = useStore(); |
| 38 | + |
| 39 | + return popup( |
| 40 | + `auth?platform=${provider.toLowerCase()}` + |
| 41 | + (store.authToken ? `&token=${store.authToken}` : "") + |
| 42 | + (isLink ? "&link_connection=true" : ""), |
| 43 | + ); |
23 | 44 | }
|
24 | 45 |
|
25 |
| - function logout() { |
26 |
| - fetch(import.meta.env.VITE_APP_API_REST + "/auth/logout", { |
27 |
| - method: "POST", |
28 |
| - credentials: "include", |
29 |
| - }) |
30 |
| - .then(() => { |
31 |
| - log.info("Signed out"); |
32 |
| - }) |
33 |
| - .catch((err) => log.error("failed to sign out", err.message)); |
| 46 | + function logout(): Promise<void> { |
| 47 | + const store = useStore(); |
| 48 | + return popup("auth/logout" + (store.authToken ? `?token=${store.authToken}` : "")); |
34 | 49 | }
|
35 | 50 |
|
36 | 51 | return {
|
|
0 commit comments