@phygitallabs/tapquest-core 4.5.0 → 4.6.0
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/bun.lock +1068 -0
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +216 -23
- package/dist/index.d.ts +216 -23
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/src/constants/firebase.ts +36 -0
- package/src/index.ts +4 -0
- package/src/modules/achievement/hooks/index.ts +0 -94
- package/src/modules/achivementWithReward/hooks/achivementPlusRewardModel.ts +30 -7
- package/src/modules/auth/hooks/useGoogleLogin.ts +5 -2
- package/src/modules/auth/hooks/useTokenRefresher.ts +3 -8
- package/src/modules/auth/providers/AuthProvider.tsx +3 -3
- package/src/modules/auth/services/FirebaseAuthService.ts +290 -0
- package/src/modules/auth/services/authServiceFactory.ts +22 -0
- package/src/modules/auth/services/index.ts +3 -0
- package/src/modules/auth/store/authStore.ts +30 -24
- package/src/modules/auth/utils/user.ts +11 -1
- package/src/modules/data-tracking/hooks/index.ts +2 -1
- package/src/modules/generate-certificate/hooks/index.ts +2 -0
- package/src/modules/reward/hooks/useRewardService.ts +1 -1
- package/src/modules/reward/types/requests.ts +1 -1
- package/src/providers/ServicesProvider.tsx +38 -2
- package/tsup.config.ts +10 -0
- package/src/store/hooks.ts +0 -6
- package/src/store/index.ts +0 -45
|
@@ -129,7 +129,7 @@ const initialState: AuthState = {
|
|
|
129
129
|
};
|
|
130
130
|
|
|
131
131
|
// Create the auth store
|
|
132
|
-
export const useAuthStore
|
|
132
|
+
export const useAuthStore = create<AuthStore>()(
|
|
133
133
|
devtools(
|
|
134
134
|
persist(
|
|
135
135
|
subscribeWithSelector(
|
|
@@ -203,14 +203,14 @@ export const useAuthStore :any = create<AuthStore>()(
|
|
|
203
203
|
return response;
|
|
204
204
|
} catch (error: any) {
|
|
205
205
|
// Check if error has code 7 (email not verified)
|
|
206
|
-
if (error?.response?.data?.code === 7) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
206
|
+
// if (error?.response?.data?.code === 7) {
|
|
207
|
+
// // Return a special response for email verification needed
|
|
208
|
+
// return {
|
|
209
|
+
// data: undefined,
|
|
210
|
+
// message: "Email verification required",
|
|
211
|
+
// code: 7,
|
|
212
|
+
// };
|
|
213
|
+
// }
|
|
214
214
|
|
|
215
215
|
const errorMessage =
|
|
216
216
|
error instanceof Error ? error.message : "Login failed";
|
|
@@ -309,19 +309,18 @@ export const useAuthStore :any = create<AuthStore>()(
|
|
|
309
309
|
state.isSignedIn = false;
|
|
310
310
|
state.error = null;
|
|
311
311
|
});
|
|
312
|
+
|
|
313
|
+
const refreshToken = tokenStorage.getRefreshToken();
|
|
312
314
|
const isTokenExpired = tokenStorage.isTokenExpired();
|
|
313
|
-
|
|
315
|
+
|
|
316
|
+
if (isTokenExpired || !refreshToken) {
|
|
314
317
|
tokenStorage.clearTokens();
|
|
315
|
-
localStorage.clear();
|
|
316
318
|
return;
|
|
317
319
|
}
|
|
318
|
-
const refreshToken = tokenStorage.getRefreshToken();
|
|
319
|
-
if (refreshToken) {
|
|
320
|
-
await authService.logout({ refreshToken });
|
|
321
|
-
}
|
|
322
320
|
|
|
323
|
-
|
|
324
|
-
|
|
321
|
+
await authService.logout({
|
|
322
|
+
refreshToken,
|
|
323
|
+
});
|
|
325
324
|
tokenStorage.clearTokens();
|
|
326
325
|
|
|
327
326
|
// Execute default cleanup functions
|
|
@@ -485,6 +484,14 @@ export const useAuthStore :any = create<AuthStore>()(
|
|
|
485
484
|
refreshToken:
|
|
486
485
|
refreshToken ?? tokenStorage.getRefreshToken() ?? "",
|
|
487
486
|
});
|
|
487
|
+
|
|
488
|
+
if(refreshTokenResponse.data?.idToken && refreshTokenResponse.data?.refreshToken){
|
|
489
|
+
tokenStorage.setTokens({
|
|
490
|
+
idToken: refreshTokenResponse.data?.idToken,
|
|
491
|
+
refreshToken: refreshTokenResponse.data?.refreshToken,
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
|
|
488
495
|
return refreshTokenResponse;
|
|
489
496
|
},
|
|
490
497
|
|
|
@@ -561,12 +568,12 @@ export const useAuthStore :any = create<AuthStore>()(
|
|
|
561
568
|
);
|
|
562
569
|
|
|
563
570
|
export const useAuth = () => {
|
|
564
|
-
const user = useAuthStore((state
|
|
565
|
-
const isSignedIn = useAuthStore((state
|
|
566
|
-
const isInitialized = useAuthStore((state
|
|
567
|
-
const isLoading = useAuthStore((state
|
|
568
|
-
const error = useAuthStore((state
|
|
569
|
-
const actions = useAuthStore((state
|
|
571
|
+
const user = useAuthStore((state) => state.user);
|
|
572
|
+
const isSignedIn = useAuthStore((state) => state.isSignedIn);
|
|
573
|
+
const isInitialized = useAuthStore((state) => state.isInitialized);
|
|
574
|
+
const isLoading = useAuthStore((state) => state.isLoading);
|
|
575
|
+
const error = useAuthStore((state) => state.error);
|
|
576
|
+
const actions = useAuthStore((state) => state.actions);
|
|
570
577
|
|
|
571
578
|
return {
|
|
572
579
|
user,
|
|
@@ -577,4 +584,3 @@ export const useAuth = () => {
|
|
|
577
584
|
...actions,
|
|
578
585
|
};
|
|
579
586
|
};
|
|
580
|
-
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { UserData, UserRole, UserType } from "../types/user-data";
|
|
2
2
|
import { UserModel } from "@phygitallabs/api-core";
|
|
3
|
-
import
|
|
3
|
+
import jwtDecode from "jwt-decode";
|
|
4
4
|
|
|
5
5
|
export const transformProtoUserData = (user: UserModel, accessToken: string): UserData => {
|
|
6
6
|
const userData = { ...user } as UserData;
|
|
@@ -17,5 +17,15 @@ export const transformProtoUserData = (user: UserModel, accessToken: string): Us
|
|
|
17
17
|
userData.roles = tokenDecoded?.roles || [UserRole.USER];
|
|
18
18
|
userData.account_type = tokenDecoded?.account_type || UserType.C;
|
|
19
19
|
userData.picture = user.picture || "";
|
|
20
|
+
|
|
20
21
|
return userData;
|
|
21
22
|
};
|
|
23
|
+
|
|
24
|
+
export const decodeJWTToken = (token: string): any => {
|
|
25
|
+
try {
|
|
26
|
+
return jwtDecode(token);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.warn("Failed to decode token in decodeJWTToken:", error);
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -47,7 +47,8 @@ function useDataTracking() {
|
|
|
47
47
|
const { setUserId, setMetadata } = useSessionReplay();
|
|
48
48
|
|
|
49
49
|
const trackEvent = (eventName: string, eventData: Record<string, any>, useTools?: ("gtm" | "ga" | "posthog")[]) => {
|
|
50
|
-
|
|
50
|
+
console.log("trackEvent ", eventName, eventData);
|
|
51
|
+
|
|
51
52
|
useTools = useTools || ["gtm"];
|
|
52
53
|
|
|
53
54
|
if (useTools.includes("gtm") && typeof window !== "undefined") {
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
useCreateCertificate,
|
|
6
6
|
useCreateCertificateAnonymous,
|
|
7
7
|
useCreateCertificateWithMask,
|
|
8
|
+
useAddFrame,
|
|
8
9
|
} from "@phygitallabs/generate-certificate";
|
|
9
10
|
|
|
10
11
|
export {
|
|
@@ -14,4 +15,5 @@ export {
|
|
|
14
15
|
useCreateCertificate,
|
|
15
16
|
useCreateCertificateAnonymous,
|
|
16
17
|
useCreateCertificateWithMask,
|
|
18
|
+
useAddFrame,
|
|
17
19
|
};
|
|
@@ -3,11 +3,11 @@ export {
|
|
|
3
3
|
useManyUserRewards,
|
|
4
4
|
useGetUserRewards,
|
|
5
5
|
useClaimUserReward,
|
|
6
|
-
useListRewardModels,
|
|
7
6
|
useGetRewardModel,
|
|
8
7
|
useCreateRewardModel,
|
|
9
8
|
useUpdateRewardModel,
|
|
10
9
|
useDeleteRewardModel,
|
|
10
|
+
useListRewardModels,
|
|
11
11
|
useCreateModelGroupReward,
|
|
12
12
|
useClearUserRewardCache,
|
|
13
13
|
useV1ListRewards
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Define request types locally since they're not exported from reward package
|
|
2
|
-
import { PaginationRequest, GetManyResponse } from "@phygitallabs/api-core
|
|
2
|
+
import { PaginationRequest, GetManyResponse } from "@phygitallabs/api-core";
|
|
3
3
|
import { EntityRewardModel, UserReward } from "./reward";
|
|
4
4
|
|
|
5
5
|
// User Rewards API Requests
|
|
@@ -10,10 +10,19 @@ import { APIConfig, ServiceConfig } from "../types/service";
|
|
|
10
10
|
|
|
11
11
|
import { checkDeviceUid } from "../modules/auth/helpers";
|
|
12
12
|
|
|
13
|
+
import { useAuth } from "../modules/auth/store/authStore";
|
|
14
|
+
|
|
15
|
+
import { httpMaxRetries, retryAttemptsRefreshToken } from "../modules/auth/constants";
|
|
16
|
+
import { getFromLS, setToLS } from "@phygitallabs/helpers";
|
|
17
|
+
import { tokenStorage } from "@phygitallabs/authentication";
|
|
18
|
+
|
|
19
|
+
import axios from "axios";
|
|
20
|
+
|
|
13
21
|
interface ServicesProviderProps {
|
|
14
22
|
children: React.ReactNode;
|
|
15
23
|
queryClient: QueryClient;
|
|
16
24
|
apiConfig: APIConfig;
|
|
25
|
+
firebaseConfig?: any;
|
|
17
26
|
}
|
|
18
27
|
|
|
19
28
|
export const ServicesProvider: React.FC<ServicesProviderProps> = ({
|
|
@@ -24,6 +33,7 @@ export const ServicesProvider: React.FC<ServicesProviderProps> = ({
|
|
|
24
33
|
version: "v1"
|
|
25
34
|
},
|
|
26
35
|
}) => {
|
|
36
|
+
const { signOut, refreshToken } = useAuth();
|
|
27
37
|
const { environment, version } = apiConfig;
|
|
28
38
|
const [commonServiceConfig, setCommonServiceConfig] = useState<ServiceConfig | null>(null);
|
|
29
39
|
|
|
@@ -35,7 +45,33 @@ export const ServicesProvider: React.FC<ServicesProviderProps> = ({
|
|
|
35
45
|
|
|
36
46
|
const responseInterceptors = ({
|
|
37
47
|
onFulfilled: (response: any) => response,
|
|
38
|
-
onRejected:
|
|
48
|
+
onRejected: async (error: any) => {
|
|
49
|
+
const originalRequest = error.config;
|
|
50
|
+
const token = tokenStorage.getAuthToken();
|
|
51
|
+
|
|
52
|
+
if (error.response?.status === 401 && !originalRequest._retry) {
|
|
53
|
+
const retryAttempts = parseInt(getFromLS(retryAttemptsRefreshToken) || "0", 10);
|
|
54
|
+
if (retryAttempts >= httpMaxRetries) {
|
|
55
|
+
await signOut();
|
|
56
|
+
setToLS(retryAttemptsRefreshToken, 0);
|
|
57
|
+
return Promise.reject(error);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setToLS(retryAttemptsRefreshToken, `${retryAttempts + 1}`);
|
|
61
|
+
originalRequest._retry = true;
|
|
62
|
+
if (token) {
|
|
63
|
+
try {
|
|
64
|
+
const result = await refreshToken();
|
|
65
|
+
if (result?.data?.idToken) {
|
|
66
|
+
originalRequest.headers.Authorization = `Bearer ${result?.data?.idToken}`;
|
|
67
|
+
return axios(originalRequest);
|
|
68
|
+
}
|
|
69
|
+
} catch (refreshError) {
|
|
70
|
+
console.log("Failed to refresh token:", refreshError);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
39
75
|
})
|
|
40
76
|
|
|
41
77
|
const requestInterceptors = ({
|
|
@@ -97,4 +133,4 @@ export const ServicesProvider: React.FC<ServicesProviderProps> = ({
|
|
|
97
133
|
</RewardServiceProvider>
|
|
98
134
|
</PGLCoreServiceProvider>
|
|
99
135
|
);
|
|
100
|
-
};
|
|
136
|
+
};
|
package/tsup.config.ts
CHANGED
|
@@ -10,6 +10,16 @@ export default defineConfig((options: Options) => ({
|
|
|
10
10
|
outExtension: ({ format }) => ({ js: format === "cjs" ? ".cjs" : ".js" }),
|
|
11
11
|
dts: true,
|
|
12
12
|
clean: true,
|
|
13
|
+
external: [
|
|
14
|
+
"react",
|
|
15
|
+
"@phygitallabs/achievement",
|
|
16
|
+
"@phygitallabs/api-core",
|
|
17
|
+
"@phygitallabs/authentication",
|
|
18
|
+
"@phygitallabs/generate-certificate",
|
|
19
|
+
"@phygitallabs/helpers",
|
|
20
|
+
"@phygitallabs/notification-api",
|
|
21
|
+
"@phygitallabs/reward",
|
|
22
|
+
],
|
|
13
23
|
sourcemap: true,
|
|
14
24
|
minify: true,
|
|
15
25
|
treeshake: true,
|
package/src/store/hooks.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
|
|
2
|
-
import type { TapquestCoreRootState, TapquestCoreAppDispatch } from './index';
|
|
3
|
-
|
|
4
|
-
// Typed hooks for internal use within tapquest-core
|
|
5
|
-
export const useAppDispatch = () => useDispatch<TapquestCoreAppDispatch>();
|
|
6
|
-
export const useAppSelector: TypedUseSelectorHook<TapquestCoreRootState> = useSelector;
|
package/src/store/index.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { configureStore } from "@reduxjs/toolkit";
|
|
2
|
-
import storage from "redux-persist/lib/storage";
|
|
3
|
-
import { persistReducer, persistStore, PersistConfig } from "redux-persist";
|
|
4
|
-
import { authSlice } from "../modules/auth/store/authSlice";
|
|
5
|
-
|
|
6
|
-
// Auth persist config
|
|
7
|
-
const authPersistConfig: PersistConfig<any> = {
|
|
8
|
-
key: "tapquest-auth",
|
|
9
|
-
storage,
|
|
10
|
-
whitelist: ["user", "isSignedIn"], // Only persist user and sign-in status
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// Create persisted auth reducer
|
|
14
|
-
const persistedAuthReducer = persistReducer(authPersistConfig, authSlice.reducer);
|
|
15
|
-
|
|
16
|
-
// Configure the tapquest-core store
|
|
17
|
-
export const store = configureStore({
|
|
18
|
-
reducer: {
|
|
19
|
-
auth: persistedAuthReducer,
|
|
20
|
-
},
|
|
21
|
-
middleware: (getDefaultMiddleware) =>
|
|
22
|
-
getDefaultMiddleware({
|
|
23
|
-
serializableCheck: {
|
|
24
|
-
// Ignore redux-persist actions
|
|
25
|
-
ignoredActions: [
|
|
26
|
-
'persist/FLUSH',
|
|
27
|
-
'persist/REHYDRATE',
|
|
28
|
-
'persist/PAUSE',
|
|
29
|
-
'persist/PERSIST',
|
|
30
|
-
'persist/PURGE',
|
|
31
|
-
'persist/REGISTER',
|
|
32
|
-
],
|
|
33
|
-
},
|
|
34
|
-
}),
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Create persistor
|
|
38
|
-
export const persistor = persistStore(store);
|
|
39
|
-
|
|
40
|
-
// Export types
|
|
41
|
-
export type TapquestCoreRootState = ReturnType<typeof store.getState>;
|
|
42
|
-
export type TapquestCoreAppDispatch = typeof store.dispatch;
|
|
43
|
-
|
|
44
|
-
// Export store instance for internal use
|
|
45
|
-
export { store as tapquestCoreStore };
|