@phygitallabs/tapquest-core 2.13.1 → 3.3.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.
@@ -1,6 +1,6 @@
1
1
  import { Achievement, useManyAchievements } from "@phygitallabs/achievement";
2
2
  import { EntityRewardModel } from "@phygitallabs/reward";
3
- import { useCreateModelGroupReward } from "@phygitallabs/reward/src/hooks/useGroupReward";
3
+ import { useCreateModelGroupReward } from "@phygitallabs/reward";
4
4
  import { useEffect, useMemo } from "react";
5
5
 
6
6
  interface UseAchivementPlusRewardModelParams {
@@ -315,7 +315,10 @@ export const useAuthStore :any = create<AuthStore>()(
315
315
  localStorage.clear();
316
316
  return;
317
317
  }
318
- await authService.logout();
318
+ const refreshToken = tokenStorage.getRefreshToken();
319
+ if (refreshToken) {
320
+ await authService.logout({ refreshToken });
321
+ }
319
322
 
320
323
  // Clear tokenStorage first
321
324
  localStorage.clear();
@@ -14,7 +14,6 @@ interface ServicesProviderProps {
14
14
  children: React.ReactNode;
15
15
  queryClient: QueryClient;
16
16
  apiConfig: APIConfig;
17
- firebaseConfig?: any;
18
17
  }
19
18
 
20
19
  export const ServicesProvider: React.FC<ServicesProviderProps> = ({
package/tsup.config.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import { defineConfig } from "tsup";
2
2
 
3
3
  export default defineConfig({
4
- entry: ["index.ts"],
4
+ entry: ["src/index.ts"],
5
5
  format: ["esm"],
6
6
  outExtension: () => ({ js: '.mjs' }),
7
7
  dts: true,
8
8
  splitting: false,
9
9
  sourcemap: true,
10
+ minify: true,
10
11
  clean: true,
11
12
  });
@@ -1,36 +0,0 @@
1
- import { EnvironmentType } from "../types/common";
2
-
3
- const FIREBASE_ENV = {
4
- dev:{
5
- "apiKey": "AIzaSyBFReJURBrU3hyiVvHHhMKcruiyy3mkEks",
6
- "authDomain": "phy-nomion-staging.firebaseapp.com",
7
- "projectId": "phy-nomion-staging",
8
- "storageBucket": "phy-nomion-staging.appspot.com",
9
- "messagingSenderId": "445937266499",
10
- "appId": "1:445937266499:web:9ce9b49c13dcecd9b889f6",
11
- "measurementId": "G-GWYSS45K15"
12
- },
13
- staging:{
14
- "apiKey": "AIzaSyAXrJB8n4eZyq43kb9pUSelz9vfkJLHRK8",
15
- "authDomain": "phygital-388705.firebaseapp.com",
16
- "projectId": "phygital-388705",
17
- "storageBucket": "assets-fygito",
18
- "messagingSenderId": "174350081236",
19
- "appId": "1:174350081236:web:da29144bf47cf775a5af41",
20
- "measurementId": "G-JVHVWXMDC9"
21
- },
22
- production:{
23
- "apiKey": "AIzaSyAXrJB8n4eZyq43kb9pUSelz9vfkJLHRK8",
24
- "authDomain": "phygital-388705.firebaseapp.com",
25
- "projectId": "phygital-388705",
26
- "storageBucket": "assets-fygito",
27
- "messagingSenderId": "174350081236",
28
- "appId": "1:174350081236:web:da29144bf47cf775a5af41",
29
- "measurementId": "G-JVHVWXMDC9"
30
- },
31
- }
32
-
33
- export const firebaseConfig = (env: EnvironmentType) => {
34
- return FIREBASE_ENV[env];
35
- };
36
-
@@ -1,290 +0,0 @@
1
- // Client-side only imports
2
- let firebaseApp: any;
3
- let firebaseAuth: any;
4
-
5
- // Dynamic imports for client-side only
6
- const getFirebaseModules = async () => {
7
- if (typeof window === 'undefined') {
8
- throw new Error('Firebase can only be used in client environment');
9
- }
10
-
11
- if (!firebaseApp || !firebaseAuth) {
12
- const [
13
- { initializeApp, getApps },
14
- {
15
- getAuth,
16
- signInWithEmailAndPassword,
17
- createUserWithEmailAndPassword,
18
- signInWithPopup,
19
- GoogleAuthProvider,
20
- signOut,
21
- sendPasswordResetEmail,
22
- sendEmailVerification,
23
- updatePassword,
24
- onAuthStateChanged,
25
- }
26
- ] = await Promise.all([
27
- import("firebase/app"),
28
- import("firebase/auth")
29
- ]);
30
-
31
- firebaseApp = { initializeApp, getApps };
32
- firebaseAuth = {
33
- getAuth,
34
- signInWithEmailAndPassword,
35
- createUserWithEmailAndPassword,
36
- signInWithPopup,
37
- GoogleAuthProvider,
38
- signOut,
39
- sendPasswordResetEmail,
40
- sendEmailVerification,
41
- updatePassword,
42
- onAuthStateChanged,
43
- };
44
- }
45
-
46
- return { firebaseApp, firebaseAuth };
47
- };
48
-
49
- import { AuthService, FirebaseConfig, AuthResponse, UserData, SignInProvider } from "../types";
50
- import { accessTokenKey, refreshTokenKey } from "../constants";
51
-
52
- export class FirebaseAuthService implements AuthService {
53
- private app: any;
54
- private auth: any;
55
- private googleProvider: any;
56
- private config: FirebaseConfig;
57
- private initialized = false;
58
-
59
- constructor(config: FirebaseConfig) {
60
- this.config = config;
61
- // Initialization will be done lazily in client environment
62
- }
63
-
64
- private async ensureInitialized() {
65
- if (this.initialized) return;
66
-
67
- const { firebaseApp, firebaseAuth } = await getFirebaseModules();
68
-
69
- // Initialize Firebase app if not already initialized
70
- if (!firebaseApp.getApps().length) {
71
- this.app = firebaseApp.initializeApp(this.config);
72
- } else {
73
- this.app = firebaseApp.getApps()[0];
74
- }
75
-
76
- this.auth = firebaseAuth.getAuth(this.app);
77
- this.googleProvider = new firebaseAuth.GoogleAuthProvider();
78
- this.initialized = true;
79
- }
80
-
81
- private transformUserData(user: any): UserData {
82
- return {
83
- id: user.uid,
84
- uid: user.uid,
85
- userName: user.displayName || "",
86
- displayName: user.displayName || "",
87
- email: user.email || "",
88
- refreshToken: user.refreshToken,
89
- accessToken: (user as any).accessToken || "",
90
- exp: (user as any).stsTokenManager?.expirationTime || 0,
91
- emailVerified: user.emailVerified,
92
- avatar: user.photoURL || "/images/default-avatar.jpg",
93
- signInProvider: this.getSignInProvider(user),
94
- role: undefined,
95
- scanStatus: false,
96
- };
97
- }
98
-
99
- private getSignInProvider(user: any): SignInProvider {
100
- const providers = user.providerData.map((p: any) => p.providerId);
101
- if (providers.includes("google.com")) return "google.com";
102
- return "password";
103
- }
104
-
105
- private translateErrorCode(errorCode: string): string {
106
- switch (errorCode) {
107
- case "auth/invalid-email":
108
- return "Email không hợp lệ";
109
- case "auth/user-disabled":
110
- return "Tài khoản đã bị khóa";
111
- case "auth/wrong-password":
112
- return "Tài khoản/mật khẩu không đúng";
113
- case "auth/user-not-found":
114
- return "Tài khoản/mật khẩu không đúng";
115
- case "auth/weak-password":
116
- return "Weak password! Please use stronger password.";
117
- case "auth/email-already-in-use":
118
- return "Email đã được sử dụng";
119
- case "auth/account-exists-with-different-credential":
120
- return "Tài khoản email đã được sử dụng bởi một phương thức đăng nhập khác";
121
- case "auth/email-not-verified":
122
- return "Email chưa được xác thực";
123
- default:
124
- return "Đã có lỗi xảy ra";
125
- }
126
- }
127
-
128
- async signInWithEmailAndPassword(email: string, password: string): Promise<AuthResponse> {
129
- try {
130
- await this.ensureInitialized();
131
- const { firebaseAuth } = await getFirebaseModules();
132
- const signInResponse = await firebaseAuth.signInWithEmailAndPassword(this.auth, email, password);
133
- const { user } = signInResponse;
134
- const data = this.transformUserData(user);
135
-
136
- const { emailVerified } = data;
137
- if (!emailVerified) {
138
- await this.signOut();
139
- localStorage.removeItem("phygital-user-info");
140
- localStorage.removeItem(accessTokenKey);
141
- localStorage.removeItem(refreshTokenKey);
142
- throw new Error("Email is not verified");
143
- }
144
-
145
- return {
146
- errorCode: "",
147
- data,
148
- };
149
- } catch (err: any) {
150
- const errorCode = this.translateErrorCode(err.code);
151
- return {
152
- errorCode,
153
- data: null,
154
- };
155
- }
156
- }
157
-
158
- async signInWithGoogle(): Promise<AuthResponse> {
159
- try {
160
- await this.ensureInitialized();
161
- const { firebaseAuth } = await getFirebaseModules();
162
- const signInResponse = await firebaseAuth.signInWithPopup(this.auth, this.googleProvider);
163
- const { user } = signInResponse;
164
- const data = this.transformUserData(user);
165
-
166
- const { emailVerified } = data;
167
- if (!emailVerified) {
168
- await this.signOut();
169
- throw new Error("Email is not verified");
170
- }
171
-
172
- return {
173
- errorCode: "",
174
- data,
175
- };
176
- } catch (err: any) {
177
- const errorCode = this.translateErrorCode(err.code);
178
- return {
179
- errorCode,
180
- data: null,
181
- };
182
- }
183
- }
184
-
185
- async signUp(email: string, password: string): Promise<AuthResponse> {
186
- try {
187
- await this.ensureInitialized();
188
- const { firebaseAuth } = await getFirebaseModules();
189
- const signUpResponse = await firebaseAuth.createUserWithEmailAndPassword(this.auth, email, password);
190
- const { user } = signUpResponse;
191
- const data = this.transformUserData(user);
192
-
193
- return {
194
- errorCode: "",
195
- data,
196
- };
197
- } catch (err: any) {
198
- const errorCode = this.translateErrorCode(err.code);
199
- return {
200
- errorCode,
201
- data: null,
202
- };
203
- }
204
- }
205
-
206
- async signOut(): Promise<void> {
207
- try {
208
- await this.ensureInitialized();
209
- const { firebaseAuth } = await getFirebaseModules();
210
- await firebaseAuth.signOut(this.auth);
211
- // Tracking is now handled through callbacks in the consuming app
212
- } catch (err) {
213
- console.log("Firebase signOut error:", err);
214
- }
215
- }
216
-
217
- async sendPasswordResetEmail(email: string): Promise<void> {
218
- await this.ensureInitialized();
219
- const { firebaseAuth } = await getFirebaseModules();
220
- const actionCodeSettings = {
221
- url: window.location.origin,
222
- handleCodeInApp: true,
223
- };
224
- await firebaseAuth.sendPasswordResetEmail(this.auth, email, actionCodeSettings);
225
- }
226
-
227
- async sendEmailVerification(): Promise<void> {
228
- await this.ensureInitialized();
229
- const { firebaseAuth } = await getFirebaseModules();
230
- if (!this.auth.currentUser) {
231
- throw new Error("No current user");
232
- }
233
- const actionCodeSettings = {
234
- url: window.location.origin,
235
- handleCodeInApp: true,
236
- };
237
- await firebaseAuth.sendEmailVerification(this.auth.currentUser, actionCodeSettings);
238
- }
239
-
240
- async changePassword(newPassword: string): Promise<void> {
241
- await this.ensureInitialized();
242
- const { firebaseAuth } = await getFirebaseModules();
243
- const user = this.auth.currentUser;
244
- if (!user) {
245
- throw new Error("No current user");
246
- }
247
- return firebaseAuth.updatePassword(user, newPassword);
248
- }
249
-
250
- onAuthStateChanged(callback: (user: UserData | null) => void): () => void {
251
- // For client-side only usage, ensure initialization before setting up auth state listener
252
- if (typeof window === 'undefined') {
253
- return () => { }; // Return empty unsubscriber for server-side
254
- }
255
-
256
- // Set up the listener asynchronously
257
- let unsubscriber: (() => void) | null = null;
258
-
259
- this.ensureInitialized().then(async () => {
260
- const { firebaseAuth } = await getFirebaseModules();
261
- unsubscriber = firebaseAuth.onAuthStateChanged(this.auth, (user: any) => {
262
- if (user && user.emailVerified) {
263
- const userData = this.transformUserData(user);
264
- callback(userData);
265
- } else {
266
- callback(null);
267
- }
268
- });
269
- });
270
-
271
- // Return unsubscriber function
272
- return () => {
273
- if (unsubscriber) {
274
- unsubscriber();
275
- }
276
- };
277
- }
278
-
279
- getCurrentUser(): UserData | null {
280
- if (typeof window === 'undefined' || !this.initialized) {
281
- return null; // Return null for server-side or before initialization
282
- }
283
-
284
- const user = this.auth.currentUser;
285
- if (user && user.emailVerified) {
286
- return this.transformUserData(user);
287
- }
288
- return null;
289
- }
290
- }
@@ -1,22 +0,0 @@
1
- import { FirebaseAuthService } from "./FirebaseAuthService";
2
-
3
- interface AuthServiceConfig {
4
- firebaseConfig: any;
5
- }
6
-
7
- let authServiceInstance: FirebaseAuthService | null = null;
8
-
9
- export const createAuthService = (config: AuthServiceConfig): FirebaseAuthService => {
10
- if (!authServiceInstance) {
11
- authServiceInstance = new FirebaseAuthService(config.firebaseConfig);
12
- }
13
- return authServiceInstance;
14
- };
15
-
16
- export const getAuthService = (): FirebaseAuthService | null => {
17
- return authServiceInstance;
18
- };
19
-
20
- export const resetAuthService = (): void => {
21
- authServiceInstance = null;
22
- };
@@ -1,3 +0,0 @@
1
- // Export Firebase auth service separately to avoid loading Firebase modules during SSR
2
- export { FirebaseAuthService } from './FirebaseAuthService';
3
- export { createAuthService, getAuthService, resetAuthService } from './authServiceFactory';