@opexa/portal-components 0.1.66 → 0.1.69
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Capacitor } from '@capacitor/core';
|
|
2
|
-
import { API_VERSION_HEADER_KEY, AUTH_API_BASE_URL_HEADER_KEY, SESSION_VERSION_HEADER_KEY, } from '../../constants/index.js';
|
|
2
|
+
import { API_VERSION_HEADER_KEY, AUTH_API_BASE_URL_HEADER_KEY, PLATFORM_WEB_HEADER_KEY, SESSION_VERSION_HEADER_KEY, } from '../../constants/index.js';
|
|
3
3
|
import { httpRequest, } from '../../services/httpRequest.js';
|
|
4
4
|
const platform = Capacitor.getPlatform();
|
|
5
5
|
export async function signIn(input, options, versionSession = 'default', version = 1, authApiBaseUrl, isInplayMobileEnabled = false) {
|
|
@@ -9,9 +9,9 @@ export async function signIn(input, options, versionSession = 'default', version
|
|
|
9
9
|
: versionSession;
|
|
10
10
|
headers.set(SESSION_VERSION_HEADER_KEY, effectiveVersionSession);
|
|
11
11
|
headers.set(API_VERSION_HEADER_KEY, String(version));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
if (version === 4 && platform === 'web') {
|
|
13
|
+
headers.set(PLATFORM_WEB_HEADER_KEY, 'true');
|
|
14
|
+
}
|
|
15
15
|
if (authApiBaseUrl) {
|
|
16
16
|
headers.set(AUTH_API_BASE_URL_HEADER_KEY, authApiBaseUrl);
|
|
17
17
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
|
+
import { App } from '@capacitor/app';
|
|
2
3
|
import { useRouter } from 'next/navigation';
|
|
4
|
+
import { useEffect, useRef } from 'react';
|
|
3
5
|
import { useTimeout } from 'usehooks-ts';
|
|
4
6
|
import { useShallow } from 'zustand/shallow';
|
|
5
7
|
import { useGlobalStore } from '../../client/hooks/useGlobalStore.js';
|
|
@@ -7,13 +9,54 @@ import { useSessionHealthQuery } from '../../client/hooks/useSessionHealthQuery.
|
|
|
7
9
|
import { useSessionQuery } from '../../client/hooks/useSessionQuery.js';
|
|
8
10
|
import { useSignOutMutation } from '../../client/hooks/useSignOutMutation.js';
|
|
9
11
|
import { toaster } from '../../client/utils/toaster.js';
|
|
12
|
+
const SESSION_WATCH_INTERVAL = 30_000;
|
|
10
13
|
export function SessionWatcher() {
|
|
11
14
|
const router = useRouter();
|
|
12
15
|
const sessionQuery = useSessionQuery();
|
|
16
|
+
const isAuthenticated = sessionQuery.data?.status === 'authenticated';
|
|
17
|
+
const isAuthenticatedRef = useRef(isAuthenticated);
|
|
18
|
+
isAuthenticatedRef.current = isAuthenticated;
|
|
13
19
|
const sessionHealthQuery = useSessionHealthQuery({
|
|
14
|
-
enabled:
|
|
15
|
-
refetchInterval: 1000 * 5,
|
|
20
|
+
enabled: false,
|
|
16
21
|
});
|
|
22
|
+
const refetchSessionHealthRef = useRef(sessionHealthQuery.refetch);
|
|
23
|
+
refetchSessionHealthRef.current = sessionHealthQuery.refetch;
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
let counter = 0;
|
|
26
|
+
let timeout = null;
|
|
27
|
+
let isForeground = true;
|
|
28
|
+
let stopped = false;
|
|
29
|
+
const listener = App.addListener('appStateChange', ({ isActive }) => {
|
|
30
|
+
isForeground = isActive;
|
|
31
|
+
});
|
|
32
|
+
const assignTimeout = () => {
|
|
33
|
+
const duration = counter <= 0 ? SESSION_WATCH_INTERVAL * 1.5 : SESSION_WATCH_INTERVAL;
|
|
34
|
+
return setTimeout(async () => {
|
|
35
|
+
if (isForeground && isAuthenticatedRef.current) {
|
|
36
|
+
try {
|
|
37
|
+
await refetchSessionHealthRef.current();
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// A failed check must not stop future session checks.
|
|
41
|
+
}
|
|
42
|
+
if (stopped) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
counter += 1;
|
|
47
|
+
timeout = assignTimeout();
|
|
48
|
+
}, duration);
|
|
49
|
+
};
|
|
50
|
+
timeout = assignTimeout();
|
|
51
|
+
return () => {
|
|
52
|
+
stopped = true;
|
|
53
|
+
if (timeout) {
|
|
54
|
+
clearTimeout(timeout);
|
|
55
|
+
timeout = null;
|
|
56
|
+
}
|
|
57
|
+
void listener.then((handle) => handle.remove()).catch(() => undefined);
|
|
58
|
+
};
|
|
59
|
+
}, []);
|
|
17
60
|
const { gameLaunch, reset } = useGlobalStore(useShallow((ctx) => ({
|
|
18
61
|
gameLaunch: ctx.gameLaunch,
|
|
19
62
|
reset: ctx.reset,
|
package/dist/services/auth.js
CHANGED
|
@@ -143,16 +143,24 @@ export async function authenticate(input, options) {
|
|
|
143
143
|
throw new Error("Invalid input 'type'");
|
|
144
144
|
}
|
|
145
145
|
export async function refreshSession(options, version = 1, authApiBaseUrl) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
let url = '';
|
|
147
|
+
url = authApiBaseUrl || AUTH_ENDPOINT;
|
|
148
|
+
url =
|
|
149
|
+
version !== 1
|
|
149
150
|
? `${url}/v${version}/session/refresh`
|
|
150
|
-
: `${url}/session:refresh
|
|
151
|
+
: `${url}/session:refresh`;
|
|
152
|
+
console.log('[REFRESH SESSION]');
|
|
153
|
+
console.log({ url });
|
|
154
|
+
try {
|
|
155
|
+
return await httpRequest.json(url, {
|
|
151
156
|
...options,
|
|
152
157
|
method: 'POST',
|
|
153
158
|
});
|
|
154
159
|
}
|
|
155
160
|
catch (e) {
|
|
161
|
+
console.log('[REFRESH SESSION]');
|
|
162
|
+
console.log('Failed to refresh session');
|
|
163
|
+
console.log(e);
|
|
156
164
|
if (e instanceof Error) {
|
|
157
165
|
throw e;
|
|
158
166
|
}
|
|
@@ -166,9 +174,13 @@ export async function refreshSession(options, version = 1, authApiBaseUrl) {
|
|
|
166
174
|
}
|
|
167
175
|
}
|
|
168
176
|
export async function destroySession(options, authApiBaseUrl) {
|
|
169
|
-
|
|
177
|
+
let url = '';
|
|
178
|
+
url = authApiBaseUrl || AUTH_ENDPOINT;
|
|
179
|
+
url = `${url}/session`;
|
|
180
|
+
console.log('[DESTROY SESSION]');
|
|
181
|
+
console.log({ url });
|
|
170
182
|
try {
|
|
171
|
-
await httpRequest(
|
|
183
|
+
await httpRequest(url, {
|
|
172
184
|
...options,
|
|
173
185
|
method: 'DELETE',
|
|
174
186
|
});
|
|
@@ -180,9 +192,14 @@ export async function destroySession(options, authApiBaseUrl) {
|
|
|
180
192
|
export async function getSessionHealth(options) {
|
|
181
193
|
try {
|
|
182
194
|
const res = await fetch(`${AUTH_ENDPOINT}/session`, options);
|
|
195
|
+
if (!res.ok) {
|
|
196
|
+
console.log('[SESSION]');
|
|
197
|
+
console.log("'/session' failed");
|
|
198
|
+
}
|
|
183
199
|
return res.ok;
|
|
184
200
|
}
|
|
185
201
|
catch {
|
|
202
|
+
console.log("unknown error in '/session'");
|
|
186
203
|
/* Network errors should not be logged out */
|
|
187
204
|
return true;
|
|
188
205
|
}
|