@umituz/react-native-auth 1.0.2 → 1.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": "@umituz/react-native-auth",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Firebase Authentication wrapper for React Native apps - Secure, type-safe, and production-ready",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -9,6 +9,7 @@ export interface SignUpParams {
9
9
  email: string;
10
10
  password: string;
11
11
  displayName?: string;
12
+ username?: string;
12
13
  }
13
14
 
14
15
  export interface SignInParams {
@@ -15,11 +15,15 @@ export interface AuthConfig {
15
15
  /** Require special characters in password */
16
16
  requireSpecialChars?: boolean;
17
17
  /** Callback for user profile creation after signup */
18
- onUserCreated?: (user: any) => Promise<void> | void;
18
+ onUserCreated?: (user: any, context?: { username?: string; displayName?: string }) => Promise<void> | void;
19
19
  /** Callback for user profile update */
20
20
  onUserUpdated?: (user: any) => Promise<void> | void;
21
+ /** Callback after successful sign in */
22
+ onSignIn?: (user: any) => Promise<void> | void;
21
23
  /** Callback for sign out cleanup */
22
24
  onSignOut?: () => Promise<void> | void;
25
+ /** Callback when guest mode is enabled */
26
+ onGuestModeEnabled?: () => Promise<void> | void;
23
27
  }
24
28
 
25
29
  export const DEFAULT_AUTH_CONFIG: Required<Omit<AuthConfig, 'onUserCreated' | 'onUserUpdated' | 'onSignOut'>> = {
@@ -15,6 +15,7 @@ import {
15
15
  import type { IAuthService, SignUpParams, SignInParams } from "../../application/ports/IAuthService";
16
16
  import {
17
17
  AuthInitializationError,
18
+ AuthConfigurationError,
18
19
  AuthValidationError,
19
20
  AuthWeakPasswordError,
20
21
  AuthInvalidEmailError,
@@ -146,9 +147,13 @@ export class AuthService implements IAuthService {
146
147
  return this.auth !== null;
147
148
  }
148
149
 
149
- private getAuth(): Auth {
150
+ private getAuth(): Auth | null {
150
151
  if (!this.auth) {
151
- throw new AuthInitializationError();
152
+ /* eslint-disable-next-line no-console */
153
+ if (__DEV__) {
154
+ console.warn("Auth service is not initialized. Call initialize() first.");
155
+ }
156
+ return null;
152
157
  }
153
158
  return this.auth;
154
159
  }
@@ -158,6 +163,9 @@ export class AuthService implements IAuthService {
158
163
  */
159
164
  async signUp(params: SignUpParams): Promise<User> {
160
165
  const auth = this.getAuth();
166
+ if (!auth) {
167
+ throw new AuthInitializationError("Auth service is not initialized");
168
+ }
161
169
 
162
170
  // Validate email
163
171
  if (!params.email || !validateEmail(params.email)) {
@@ -193,7 +201,10 @@ export class AuthService implements IAuthService {
193
201
  // Call user created callback if provided
194
202
  if (this.config.onUserCreated) {
195
203
  try {
196
- await this.config.onUserCreated(userCredential.user);
204
+ await this.config.onUserCreated(userCredential.user, {
205
+ username: params.username,
206
+ displayName: params.displayName,
207
+ });
197
208
  } catch (callbackError) {
198
209
  // Don't fail signup if callback fails
199
210
  }
@@ -210,6 +221,9 @@ export class AuthService implements IAuthService {
210
221
  */
211
222
  async signIn(params: SignInParams): Promise<User> {
212
223
  const auth = this.getAuth();
224
+ if (!auth) {
225
+ throw new AuthInitializationError("Auth service is not initialized");
226
+ }
213
227
 
214
228
  // Validate email
215
229
  if (!params.email || !validateEmail(params.email)) {
@@ -229,6 +243,16 @@ export class AuthService implements IAuthService {
229
243
  );
230
244
 
231
245
  this.isGuestMode = false;
246
+
247
+ // Call sign in callback if provided
248
+ if (this.config.onSignIn) {
249
+ try {
250
+ await this.config.onSignIn(userCredential.user);
251
+ } catch (callbackError) {
252
+ // Don't fail signin if callback fails
253
+ }
254
+ }
255
+
232
256
  return userCredential.user;
233
257
  } catch (error: any) {
234
258
  throw mapFirebaseAuthError(error);
@@ -240,6 +264,11 @@ export class AuthService implements IAuthService {
240
264
  */
241
265
  async signOut(): Promise<void> {
242
266
  const auth = this.getAuth();
267
+ if (!auth) {
268
+ // If auth is not initialized, just clear guest mode
269
+ this.isGuestMode = false;
270
+ return;
271
+ }
243
272
 
244
273
  try {
245
274
  await firebaseSignOut(auth);
@@ -265,7 +294,7 @@ export class AuthService implements IAuthService {
265
294
  const auth = this.getAuth();
266
295
 
267
296
  // Sign out from Firebase if logged in
268
- if (auth.currentUser) {
297
+ if (auth && auth.currentUser) {
269
298
  try {
270
299
  await firebaseSignOut(auth);
271
300
  } catch (error) {
@@ -274,6 +303,15 @@ export class AuthService implements IAuthService {
274
303
  }
275
304
 
276
305
  this.isGuestMode = true;
306
+
307
+ // Call guest mode enabled callback if provided
308
+ if (this.config.onGuestModeEnabled) {
309
+ try {
310
+ await this.config.onGuestModeEnabled();
311
+ } catch (callbackError) {
312
+ // Don't fail guest mode if callback fails
313
+ }
314
+ }
277
315
  }
278
316
 
279
317
  /**
@@ -298,6 +336,11 @@ export class AuthService implements IAuthService {
298
336
  */
299
337
  onAuthStateChange(callback: (user: User | null) => void): () => void {
300
338
  const auth = this.getAuth();
339
+ if (!auth) {
340
+ // Return no-op unsubscribe if auth is not initialized
341
+ callback(null);
342
+ return () => {};
343
+ }
301
344
 
302
345
  return onAuthStateChanged(auth, (user) => {
303
346
  // Don't update if in guest mode
@@ -333,13 +376,17 @@ export function initializeAuthService(
333
376
 
334
377
  /**
335
378
  * Get auth service instance
336
- * @throws {AuthInitializationError} If service is not initialized
379
+ * Returns null if service is not initialized (graceful degradation)
337
380
  */
338
- export function getAuthService(): AuthService {
381
+ export function getAuthService(): AuthService | null {
339
382
  if (!authServiceInstance || !authServiceInstance.isInitialized()) {
340
- throw new AuthInitializationError(
341
- "Auth service is not initialized. Call initializeAuthService() first."
342
- );
383
+ /* eslint-disable-next-line no-console */
384
+ if (__DEV__) {
385
+ console.warn(
386
+ "Auth service is not initialized. Call initializeAuthService() first."
387
+ );
388
+ }
389
+ return null;
343
390
  }
344
391
  return authServiceInstance;
345
392
  }
@@ -40,8 +40,16 @@ export function useAuth(): UseAuthResult {
40
40
  const [isGuest, setIsGuest] = useState(false);
41
41
 
42
42
  useEffect(() => {
43
+ const service = getAuthService();
44
+ if (!service) {
45
+ // Auth service not initialized
46
+ setUser(null);
47
+ setIsGuest(false);
48
+ setLoading(false);
49
+ return () => {};
50
+ }
51
+
43
52
  try {
44
- const service = getAuthService();
45
53
  const unsubscribe = service.onAuthStateChange((currentUser) => {
46
54
  setUser(currentUser);
47
55
  setIsGuest(service.getIsGuestMode());
@@ -58,7 +66,7 @@ export function useAuth(): UseAuthResult {
58
66
  unsubscribe();
59
67
  };
60
68
  } catch (error) {
61
- // Auth service not initialized
69
+ // Auth service error
62
70
  setUser(null);
63
71
  setIsGuest(false);
64
72
  setLoading(false);
@@ -68,18 +76,27 @@ export function useAuth(): UseAuthResult {
68
76
 
69
77
  const signUp = useCallback(async (email: string, password: string, displayName?: string) => {
70
78
  const service = getAuthService();
79
+ if (!service) {
80
+ throw new Error("Auth service is not initialized");
81
+ }
71
82
  await service.signUp({ email, password, displayName });
72
83
  // State will be updated via onAuthStateChange
73
84
  }, []);
74
85
 
75
86
  const signIn = useCallback(async (email: string, password: string) => {
76
87
  const service = getAuthService();
88
+ if (!service) {
89
+ throw new Error("Auth service is not initialized");
90
+ }
77
91
  await service.signIn({ email, password });
78
92
  // State will be updated via onAuthStateChange
79
93
  }, []);
80
94
 
81
95
  const signOut = useCallback(async () => {
82
96
  const service = getAuthService();
97
+ if (!service) {
98
+ return;
99
+ }
83
100
  await service.signOut();
84
101
  setUser(null);
85
102
  setIsGuest(false);
@@ -87,6 +104,10 @@ export function useAuth(): UseAuthResult {
87
104
 
88
105
  const continueAsGuest = useCallback(async () => {
89
106
  const service = getAuthService();
107
+ if (!service) {
108
+ setIsGuest(true);
109
+ return;
110
+ }
90
111
  await service.setGuestMode();
91
112
  setUser(null);
92
113
  setIsGuest(true);