@phygitallabs/tapquest-core 4.7.0 → 6.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phygitallabs/tapquest-core",
3
- "version": "4.7.0",
3
+ "version": "6.1.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1,2 +1,6 @@
1
1
  export { useGoogleLogin } from "./useGoogleLogin";
2
- export type { UseGoogleLoginOptions, UseGoogleLoginReturn } from "./useGoogleLogin";
2
+ export type {
3
+ UseGoogleLoginOptions,
4
+ UseGoogleLoginReturn,
5
+ } from "./useGoogleLogin";
6
+ export { useTokenRefresher } from "./useTokenRefresher";
@@ -1,4 +1,4 @@
1
- import { useRef, useCallback, useEffect } from "react";
1
+ import { useRef, useCallback, useEffect, useState } from "react";
2
2
  import { useAuth } from "../store/authStore";
3
3
  import { tokenStorage } from "@phygitallabs/authentication";
4
4
  import { ALLOWED_ORIGINS } from "../constants";
@@ -12,14 +12,23 @@ export interface UseGoogleLoginOptions {
12
12
  }
13
13
 
14
14
  export interface UseGoogleLoginReturn {
15
- signIn: () => Promise<void>;
15
+ signIn: () => void;
16
16
  isLoading: boolean;
17
17
  error: string | null;
18
18
  }
19
19
 
20
- export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLoginReturn {
20
+ export function useGoogleLogin(
21
+ options: UseGoogleLoginOptions = {}
22
+ ): UseGoogleLoginReturn {
21
23
  const { onSuccess, onError, onPopupBlocked, onPopupClosed } = options;
22
- const { signInWithGoogle, refreshToken, setIsSignedIn, setIsLoading, isLoading } = useAuth();
24
+ const {
25
+ signInWithGoogle,
26
+ refreshToken,
27
+ setIsSignedIn,
28
+ setIsLoading,
29
+ isLoading,
30
+ } = useAuth();
31
+ const [googleLink, setGoogleLink] = useState<string | null>(null);
23
32
 
24
33
  const popupRef = useRef<Window | null>(null);
25
34
  const gotDataRef = useRef(false);
@@ -37,14 +46,15 @@ export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLo
37
46
  refreshToken: newToken.data.refreshToken,
38
47
  });
39
48
 
40
- const userData = decodeJWTToken(newToken.data.idToken);
49
+ const userData = decodeJWTToken(newToken.data.idToken);
41
50
 
42
51
  setIsSignedIn(true);
43
52
  setIsLoading(false);
44
53
  onSuccess?.(userData);
45
54
  }
46
55
  } catch (error) {
47
- const errorMessage = error instanceof Error ? error.message : "Token refresh failed";
56
+ const errorMessage =
57
+ error instanceof Error ? error.message : "Token refresh failed";
48
58
  errorRef.current = errorMessage;
49
59
  onError?.(errorMessage);
50
60
  setIsLoading(false);
@@ -59,6 +69,7 @@ export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLo
59
69
  clearInterval(timerRef.current);
60
70
  timerRef.current = null;
61
71
  }
72
+
62
73
  gotDataRef.current = false;
63
74
  }, []);
64
75
 
@@ -68,7 +79,10 @@ export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLo
68
79
  const allowedOrigins = ALLOWED_ORIGINS.concat(window?.location?.origin);
69
80
 
70
81
  // Allow wildcard origin from callback.html (but validate message structure)
71
- if (event.origin !== "*" && !allowedOrigins.includes(event.origin as string)) {
82
+ if (
83
+ event.origin !== "*" &&
84
+ !allowedOrigins.includes(event.origin as string)
85
+ ) {
72
86
  console.warn("Rejected message from untrusted origin:", event.origin);
73
87
  return;
74
88
  }
@@ -78,7 +92,6 @@ export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLo
78
92
  console.warn("Invalid message data received");
79
93
  return;
80
94
  }
81
-
82
95
  if (event.data.type === "LOGIN_SUCCESS") {
83
96
  const { refreshToken: refresh } = event.data;
84
97
 
@@ -100,19 +113,39 @@ export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLo
100
113
  [handleTokenRefresh, setIsLoading, cleanup, onError]
101
114
  );
102
115
 
103
- const signIn = useCallback(async (): Promise<void> => {
116
+ useEffect(() => {
117
+ const fetchGoogleLink = async () => {
118
+ try {
119
+ const response = await signInWithGoogle();
120
+ setGoogleLink(response.statusMessage || "");
121
+ } catch (error) {
122
+ console.error("error", error);
123
+ setGoogleLink("");
124
+ }
125
+ };
126
+ fetchGoogleLink();
127
+ }, []);
128
+
129
+ const signIn = useCallback((): void => {
104
130
  const width = 500;
105
131
  const height = 600;
106
132
  const left = window.screenX + (window.outerWidth - width) / 2;
107
133
  const top = window.screenY + (window.outerHeight - height) / 2;
108
134
 
109
135
  try {
136
+ if (googleLink === null) return;
137
+ if (googleLink === "") {
138
+ const error = "Google sign in failed. Please try again later.";
139
+ errorRef.current = error;
140
+ onError?.(error);
141
+ return;
142
+ }
143
+
110
144
  errorRef.current = null;
111
145
  setIsLoading(true);
112
- const response = await signInWithGoogle();
113
146
 
114
147
  popupRef.current = window.open(
115
- `${response.statusMessage}`,
148
+ googleLink,
116
149
  "oauthPopup",
117
150
  `width=${width},height=${height},left=${left},top=${top},resizable,scrollbars`
118
151
  );
@@ -143,12 +176,21 @@ export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLo
143
176
  }
144
177
  }, 500);
145
178
  } catch (error: any) {
146
- const errorMessage = error instanceof Error ? error.message : "Google sign in failed";
179
+ const errorMessage =
180
+ error instanceof Error ? error.message : "Google sign in failed";
147
181
  errorRef.current = errorMessage;
148
182
  onError?.(errorMessage);
149
183
  setIsLoading(false);
150
184
  }
151
- }, [signInWithGoogle, setIsLoading, handleMessage, onError, onPopupBlocked, onPopupClosed]);
185
+ }, [
186
+ signInWithGoogle,
187
+ setIsLoading,
188
+ handleMessage,
189
+ onError,
190
+ onPopupBlocked,
191
+ onPopupClosed,
192
+ googleLink,
193
+ ]);
152
194
 
153
195
  useEffect(() => {
154
196
  window.addEventListener("message", handleMessage);
@@ -162,7 +204,7 @@ export function useGoogleLogin(options: UseGoogleLoginOptions = {}): UseGoogleLo
162
204
  return () => {
163
205
  cleanup();
164
206
  };
165
- }, [cleanup, handleMessage]);
207
+ }, [cleanup]);
166
208
 
167
209
  return {
168
210
  signIn,
@@ -9,18 +9,19 @@ export type {
9
9
  UseAuthReturn,
10
10
  AuthProviderProps,
11
11
  AuthCallbacks,
12
- } from './types';
12
+ } from "./types";
13
13
 
14
14
  // Export hooks and providers
15
- export { AuthProvider } from './providers';
15
+ export { AuthProvider } from "./providers";
16
16
 
17
17
  // Export auth hooks from store
18
- export { useAuth } from './store/authStore';
18
+ export { useAuth, useAuthStore } from "./store/authStore";
19
19
 
20
20
  // Export auth hooks
21
- export { useGoogleLogin } from './hooks';
22
- export type { UseGoogleLoginOptions, UseGoogleLoginReturn } from './hooks';
21
+ export { useGoogleLogin, useTokenRefresher } from "./hooks";
22
+ export type { UseGoogleLoginOptions, UseGoogleLoginReturn } from "./hooks";
23
23
 
24
24
  // Export constants and helpers
25
- export * from './constants';
26
- export * from './helpers';
25
+ export * from "./constants";
26
+ export * from "./helpers";
27
+ export * from "./utils/user";
@@ -1,6 +1,6 @@
1
1
  import { UserData, UserRole, UserType } from "../types/user-data";
2
2
  import { UserModel } from "@phygitallabs/api-core";
3
- import jwtDecode from "jwt-decode";
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;
@@ -59,6 +59,7 @@ export const ServicesProvider: React.FC<ServicesProviderProps> = ({
59
59
 
60
60
  setToLS(retryAttemptsRefreshToken, `${retryAttempts + 1}`);
61
61
  originalRequest._retry = true;
62
+
62
63
  if (token) {
63
64
  try {
64
65
  const result = await refreshToken();
@@ -67,7 +68,9 @@ export const ServicesProvider: React.FC<ServicesProviderProps> = ({
67
68
  return axios(originalRequest);
68
69
  }
69
70
  } catch (refreshError) {
70
- console.log("Failed to refresh token:", refreshError);
71
+ console.error("Failed to refresh token:", refreshError);
72
+ await signOut();
73
+ return Promise.reject(refreshError);
71
74
  }
72
75
  }
73
76
  }