@umituz/react-native-auth 1.0.2 → 1.0.3
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
|
@@ -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
|
-
|
|
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)) {
|
|
@@ -210,6 +218,9 @@ export class AuthService implements IAuthService {
|
|
|
210
218
|
*/
|
|
211
219
|
async signIn(params: SignInParams): Promise<User> {
|
|
212
220
|
const auth = this.getAuth();
|
|
221
|
+
if (!auth) {
|
|
222
|
+
throw new AuthInitializationError("Auth service is not initialized");
|
|
223
|
+
}
|
|
213
224
|
|
|
214
225
|
// Validate email
|
|
215
226
|
if (!params.email || !validateEmail(params.email)) {
|
|
@@ -240,6 +251,11 @@ export class AuthService implements IAuthService {
|
|
|
240
251
|
*/
|
|
241
252
|
async signOut(): Promise<void> {
|
|
242
253
|
const auth = this.getAuth();
|
|
254
|
+
if (!auth) {
|
|
255
|
+
// If auth is not initialized, just clear guest mode
|
|
256
|
+
this.isGuestMode = false;
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
243
259
|
|
|
244
260
|
try {
|
|
245
261
|
await firebaseSignOut(auth);
|
|
@@ -265,7 +281,7 @@ export class AuthService implements IAuthService {
|
|
|
265
281
|
const auth = this.getAuth();
|
|
266
282
|
|
|
267
283
|
// Sign out from Firebase if logged in
|
|
268
|
-
if (auth.currentUser) {
|
|
284
|
+
if (auth && auth.currentUser) {
|
|
269
285
|
try {
|
|
270
286
|
await firebaseSignOut(auth);
|
|
271
287
|
} catch (error) {
|
|
@@ -298,6 +314,11 @@ export class AuthService implements IAuthService {
|
|
|
298
314
|
*/
|
|
299
315
|
onAuthStateChange(callback: (user: User | null) => void): () => void {
|
|
300
316
|
const auth = this.getAuth();
|
|
317
|
+
if (!auth) {
|
|
318
|
+
// Return no-op unsubscribe if auth is not initialized
|
|
319
|
+
callback(null);
|
|
320
|
+
return () => {};
|
|
321
|
+
}
|
|
301
322
|
|
|
302
323
|
return onAuthStateChanged(auth, (user) => {
|
|
303
324
|
// Don't update if in guest mode
|
|
@@ -333,13 +354,17 @@ export function initializeAuthService(
|
|
|
333
354
|
|
|
334
355
|
/**
|
|
335
356
|
* Get auth service instance
|
|
336
|
-
*
|
|
357
|
+
* Returns null if service is not initialized (graceful degradation)
|
|
337
358
|
*/
|
|
338
|
-
export function getAuthService(): AuthService {
|
|
359
|
+
export function getAuthService(): AuthService | null {
|
|
339
360
|
if (!authServiceInstance || !authServiceInstance.isInitialized()) {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
361
|
+
/* eslint-disable-next-line no-console */
|
|
362
|
+
if (__DEV__) {
|
|
363
|
+
console.warn(
|
|
364
|
+
"Auth service is not initialized. Call initializeAuthService() first."
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
return null;
|
|
343
368
|
}
|
|
344
369
|
return authServiceInstance;
|
|
345
370
|
}
|
|
@@ -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
|
|
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);
|