@umituz/react-native-auth 1.6.2 → 1.6.4

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": "@umituz/react-native-auth",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
4
4
  "description": "Authentication service for React Native apps - Secure, type-safe, and production-ready. Provider-agnostic design supports Firebase Auth and can be adapted for Supabase or other providers.",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -28,6 +28,9 @@ import {
28
28
  import type { AuthConfig } from "../../domain/value-objects/AuthConfig";
29
29
  import { DEFAULT_AUTH_CONFIG } from "../../domain/value-objects/AuthConfig";
30
30
  import { DeviceEventEmitter } from "react-native";
31
+ import { storageRepository } from "@umituz/react-native-storage";
32
+
33
+ const GUEST_MODE_KEY = "@auth_guest_mode";
31
34
 
32
35
  /**
33
36
  * Validate email format
@@ -138,12 +141,20 @@ export class AuthService implements IAuthService {
138
141
  * Initialize auth service with Firebase Auth instance
139
142
  * Must be called before using any auth methods
140
143
  */
141
- initialize(auth: Auth): void {
144
+ async initialize(auth: Auth): Promise<void> {
142
145
  if (!auth) {
143
146
  throw new AuthInitializationError("Auth instance is required");
144
147
  }
145
148
  this.auth = auth;
146
- this.isGuestMode = false;
149
+
150
+ // Restore guest mode from storage
151
+ try {
152
+ const guestModeValue = await storageRepository.getString(GUEST_MODE_KEY, "false");
153
+ this.isGuestMode = guestModeValue === "true";
154
+ } catch (error) {
155
+ // If storage fails, default to false
156
+ this.isGuestMode = false;
157
+ }
147
158
  }
148
159
 
149
160
  /**
@@ -207,6 +218,11 @@ export class AuthService implements IAuthService {
207
218
  // Clear guest mode when user signs up
208
219
  if (this.isGuestMode) {
209
220
  this.isGuestMode = false;
221
+ try {
222
+ await storageRepository.removeItem(GUEST_MODE_KEY);
223
+ } catch (error) {
224
+ // Ignore storage errors
225
+ }
210
226
  }
211
227
 
212
228
  // Emit event for AppNavigator to handle navigation
@@ -247,6 +263,11 @@ export class AuthService implements IAuthService {
247
263
  // Clear guest mode when user signs in
248
264
  if (this.isGuestMode) {
249
265
  this.isGuestMode = false;
266
+ try {
267
+ await storageRepository.removeItem(GUEST_MODE_KEY);
268
+ } catch (error) {
269
+ // Ignore storage errors
270
+ }
250
271
  }
251
272
 
252
273
  // Emit event for AppNavigator to handle navigation
@@ -266,13 +287,18 @@ export class AuthService implements IAuthService {
266
287
  if (!auth) {
267
288
  // If auth is not initialized, just clear guest mode
268
289
  this.isGuestMode = false;
290
+ try {
291
+ await storageRepository.removeItem(GUEST_MODE_KEY);
292
+ } catch (error) {
293
+ // Ignore storage errors
294
+ }
269
295
  return;
270
296
  }
271
297
 
272
298
  try {
273
299
  await firebaseSignOut(auth);
274
- this.isGuestMode = false;
275
-
300
+ // Don't clear guest mode on sign out - user might want to continue as guest
301
+ // Guest mode will be cleared when user signs in or signs up
276
302
  } catch (error: any) {
277
303
  throw mapFirebaseAuthError(error);
278
304
  }
@@ -295,6 +321,13 @@ export class AuthService implements IAuthService {
295
321
 
296
322
  this.isGuestMode = true;
297
323
 
324
+ // Persist guest mode to storage
325
+ try {
326
+ await storageRepository.setString(GUEST_MODE_KEY, "true");
327
+ } catch (error) {
328
+ // Ignore storage errors - guest mode will still work in memory
329
+ }
330
+
298
331
  // Emit event for AppNavigator to handle navigation
299
332
  DeviceEventEmitter.emit("guest-mode-enabled");
300
333
  }
@@ -358,17 +391,17 @@ let authServiceInstance: AuthService | null = null;
358
391
  * @param auth - Firebase Auth instance
359
392
  * @param config - Optional auth configuration (defaults to permissive settings)
360
393
  */
361
- export function initializeAuthService(
394
+ export async function initializeAuthService(
362
395
  auth: Auth,
363
396
  config?: AuthConfig
364
- ): AuthService {
397
+ ): Promise<AuthService> {
365
398
  // Use default config if not provided (permissive settings for better UX)
366
399
  const finalConfig = config || DEFAULT_AUTH_CONFIG;
367
400
 
368
401
  if (!authServiceInstance) {
369
402
  authServiceInstance = new AuthService(finalConfig);
370
403
  }
371
- authServiceInstance.initialize(auth);
404
+ await authServiceInstance.initialize(auth);
372
405
  return authServiceInstance;
373
406
  }
374
407
 
@@ -124,6 +124,10 @@ export function useAuth(): UseAuthResult {
124
124
  setLoading(true);
125
125
  setError(null);
126
126
  await service.signUp({ email, password, displayName });
127
+ // Clear guest mode when user signs up
128
+ if (isGuest) {
129
+ setIsGuest(false);
130
+ }
127
131
  // User state is updated by Firebase Auth's onAuthStateChanged
128
132
  } catch (err: any) {
129
133
  const errorMessage = err.message || "Sign up failed";
@@ -132,7 +136,7 @@ export function useAuth(): UseAuthResult {
132
136
  } finally {
133
137
  setLoading(false);
134
138
  }
135
- }, []);
139
+ }, [isGuest]);
136
140
 
137
141
  const signIn = useCallback(async (email: string, password: string) => {
138
142
  const service = getAuthService();
@@ -145,6 +149,10 @@ export function useAuth(): UseAuthResult {
145
149
  setLoading(true);
146
150
  setError(null);
147
151
  await service.signIn({ email, password });
152
+ // Clear guest mode when user signs in
153
+ if (isGuest) {
154
+ setIsGuest(false);
155
+ }
148
156
  // User state is updated by Firebase Auth's onAuthStateChanged
149
157
  } catch (err: any) {
150
158
  const errorMessage = err.message || "Failed to sign in";
@@ -153,7 +161,7 @@ export function useAuth(): UseAuthResult {
153
161
  } finally {
154
162
  setLoading(false);
155
163
  }
156
- }, []);
164
+ }, [isGuest]);
157
165
 
158
166
  const signOut = useCallback(async () => {
159
167
  const service = getAuthService();