|
4 | 4 | import { Button } from '$lib/components/ui/button/index.js'; |
5 | 5 | import { userState, userToken } from '$lib/store/LocalStorage.svelte'; |
6 | 6 | import { env } from '$env/dynamic/public'; |
7 | | - import { onMount } from 'svelte'; |
| 7 | + import { onMount, onDestroy } from 'svelte'; |
| 8 | + import { toast } from 'svelte-sonner'; |
| 9 | +
|
| 10 | + type LoginPayload = { |
| 11 | + token: string; |
| 12 | + id: string; |
| 13 | + login: string; |
| 14 | + name: string; |
| 15 | + stv_id?: string | null; |
| 16 | + is_channel?: boolean; |
| 17 | + pfp?: string | null; |
| 18 | + }; |
| 19 | +
|
| 20 | + const DEFAULT_LOGIN_URL = 'https://api.potat.app/login'; |
8 | 21 |
|
9 | 22 | let { open = $bindable(), empty = false } = $props(); |
| 23 | + let loginWindow: Window | null = null; |
10 | 24 |
|
11 | | - let onLogin = async (): Promise<void> => { |
12 | | - window.open( |
13 | | - env.PUBLIC_LOGIN_URL ?? 'https://api.potat.app/login', |
14 | | - '_blank', |
15 | | - 'width=600,height=400' |
16 | | - ); |
| 25 | + const isLoginPayload = (value: unknown): value is LoginPayload => { |
| 26 | + if (typeof value !== 'object' || value === null) { |
| 27 | + return false; |
| 28 | + } |
17 | 29 |
|
18 | | - // Mocked example result of postMessage from the window after successful login |
19 | | - userToken.set('mock-test-token-definitely-real'); |
20 | | - userState.set({ |
21 | | - id: '457260003', |
22 | | - login: 'ryanpotat', |
23 | | - name: 'RyanPotat', |
24 | | - stv_id: '01G6HF7Y9R000AE6YXS14X580S', |
25 | | - is_channel: false, |
26 | | - }); |
| 30 | + const payload = value as Record<string, unknown>; |
| 31 | + return typeof payload.token === 'string' && typeof payload.id === 'string'; |
| 32 | + }; |
27 | 33 |
|
28 | | - open = false; |
| 34 | + const resolveLoginUrl = (): string => { |
| 35 | + const rawUrl = env.PUBLIC_LOGIN_URL ?? DEFAULT_LOGIN_URL; |
| 36 | +
|
| 37 | + if (!browser) { |
| 38 | + return rawUrl; |
| 39 | + } |
| 40 | +
|
| 41 | + try { |
| 42 | + return new URL(rawUrl, window.location.origin).href; |
| 43 | + } catch { |
| 44 | + return DEFAULT_LOGIN_URL; |
| 45 | + } |
29 | 46 | }; |
30 | 47 |
|
31 | | - const handleMessage = (event: MessageEvent) => { |
32 | | - const { id, login, name, stv_id, token, is_channel } = event.data ?? {}; |
| 48 | + const resolveLoginOrigin = (): string => { |
| 49 | + if (!browser) { |
| 50 | + return new URL(DEFAULT_LOGIN_URL).origin; |
| 51 | + } |
| 52 | +
|
| 53 | + try { |
| 54 | + return new URL(resolveLoginUrl()).origin; |
| 55 | + } catch { |
| 56 | + return new URL(DEFAULT_LOGIN_URL).origin; |
| 57 | + } |
| 58 | + }; |
| 59 | +
|
| 60 | + const onLogin = (): void => { |
| 61 | + if (!browser) { |
| 62 | + return; |
| 63 | + } |
| 64 | +
|
| 65 | + const loginUrl = resolveLoginUrl(); |
| 66 | + loginWindow = window.open(loginUrl, 'potat-login', 'width=600,height=800'); |
| 67 | +
|
| 68 | + if (!loginWindow) { |
| 69 | + toast.error('Popup blocked', { |
| 70 | + description: 'Please allow popups for this site and try again.', |
| 71 | + duration: 5000, |
| 72 | + }); |
| 73 | + } |
| 74 | + }; |
33 | 75 |
|
34 | | - if (!token || !id) { |
| 76 | + const handleMessage = (event: MessageEvent<unknown>): void => { |
| 77 | + if (event.origin !== resolveLoginOrigin()) { |
35 | 78 | return; |
36 | 79 | } |
37 | 80 |
|
| 81 | + if (!isLoginPayload(event.data)) { |
| 82 | + return; |
| 83 | + } |
| 84 | +
|
| 85 | + const { id, login, name, stv_id, token, is_channel, pfp } = event.data; |
| 86 | +
|
38 | 87 | userToken.set(token); |
39 | 88 | userState.set({ |
40 | 89 | id, |
41 | 90 | login, |
42 | 91 | name, |
43 | | - stv_id, |
44 | | - is_channel, |
| 92 | + stv_id: stv_id ?? '', |
| 93 | + is_channel: Boolean(is_channel), |
| 94 | + pfp: pfp ?? undefined, |
45 | 95 | }); |
| 96 | +
|
| 97 | + if (!pfp) { |
| 98 | + fetch(`https://api.potat.app/users/${login}`) |
| 99 | + .then(r => r.json()) |
| 100 | + .then(res => { |
| 101 | + const connections: { platform: string; pfp?: string }[] = res?.data?.[0]?.user?.connections ?? []; |
| 102 | + const twitchConn = connections.find(c => c.platform === 'TWITCH'); |
| 103 | + if (twitchConn?.pfp) { |
| 104 | + userState.update(s => s ? { ...s, pfp: twitchConn.pfp } : s); |
| 105 | + } |
| 106 | + }) |
| 107 | + .catch(() => {/* non-fatal */}); |
| 108 | + } |
| 109 | +
|
| 110 | + open = false; |
| 111 | +
|
| 112 | + if (loginWindow && !loginWindow.closed) { |
| 113 | + loginWindow.close(); |
| 114 | + } |
| 115 | + loginWindow = null; |
46 | 116 | }; |
47 | 117 |
|
48 | 118 | onMount((): void => { |
49 | | - if (browser) { |
50 | | - window.addEventListener('message', handleMessage); |
| 119 | + if (!browser) { |
| 120 | + return; |
| 121 | + } |
| 122 | +
|
| 123 | + window.addEventListener('message', handleMessage); |
| 124 | + }); |
| 125 | +
|
| 126 | + onDestroy((): void => { |
| 127 | + if (!browser) { |
| 128 | + return; |
| 129 | + } |
| 130 | +
|
| 131 | + window.removeEventListener('message', handleMessage); |
| 132 | +
|
| 133 | + if (loginWindow && !loginWindow.closed) { |
| 134 | + loginWindow.close(); |
51 | 135 | } |
52 | 136 | }); |
53 | 137 | </script> |
54 | 138 |
|
55 | | -<AlertDialog.Root bind:open on:cancel={() => (open = false)}> |
| 139 | +<AlertDialog.Root bind:open> |
56 | 140 | <AlertDialog.Trigger> |
57 | 141 | {#if !empty} |
58 | | - <Button variant="outline" size="default"> |
59 | | - Login |
60 | | - </Button> |
| 142 | + <Button variant="outline" size="default">Login</Button> |
61 | 143 | {/if} |
62 | 144 | </AlertDialog.Trigger> |
63 | 145 |
|
64 | 146 | <AlertDialog.Content> |
65 | | - |
66 | 147 | <AlertDialog.Header> |
67 | | - <AlertDialog.Action on:click={onLogin}> |
68 | | - Sign in with Twitch |
69 | | - </AlertDialog.Action> |
| 148 | + <AlertDialog.Title>Sign in to PotatBotat</AlertDialog.Title> |
| 149 | + <AlertDialog.Description> |
| 150 | + By signing in, you agree to our |
| 151 | + <a href="/dashboard/privacy-policy">Privacy Policy</a> |
| 152 | + and <a href="/dashboard/terms">Terms of Service</a>. |
| 153 | + </AlertDialog.Description> |
70 | 154 | </AlertDialog.Header> |
71 | | - |
72 | 155 | <AlertDialog.Footer> |
73 | | - <p> |
74 | | - By signing in, you agree to our |
75 | | - <a href="/dashboard/privacy-policy"> Privacy Policy </a> |
76 | | - and |
77 | | - <a href="/dashboard/terms"> Terms of Service</a>. |
78 | | - </p> |
| 156 | + <AlertDialog.Cancel>Cancel</AlertDialog.Cancel> |
| 157 | + <AlertDialog.Action onclick={onLogin}>Sign in with Twitch</AlertDialog.Action> |
79 | 158 | </AlertDialog.Footer> |
80 | | - |
81 | 159 | </AlertDialog.Content> |
82 | 160 | </AlertDialog.Root> |
83 | 161 |
|
84 | 162 | <style> |
85 | 163 | a { |
86 | | - color: #2196f3; |
| 164 | + color: hsl(var(--primary)); |
87 | 165 | text-decoration: none; |
88 | 166 | white-space: nowrap; |
89 | 167 | } |
|
0 commit comments