@umituz/react-native-auth 3.1.9 → 3.1.11
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": "3.1.
|
|
3
|
+
"version": "3.1.11",
|
|
4
4
|
"description": "Authentication service for React Native apps - Secure, type-safe, and production-ready. Provider-agnostic design with dependency injection, configurable validation, and comprehensive error handling.",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
package/src/index.ts
CHANGED
|
@@ -105,6 +105,12 @@ export type {
|
|
|
105
105
|
PasswordRequirements,
|
|
106
106
|
} from './infrastructure/utils/AuthValidation';
|
|
107
107
|
|
|
108
|
+
// =============================================================================
|
|
109
|
+
// PRESENTATION LAYER - Provider
|
|
110
|
+
// =============================================================================
|
|
111
|
+
|
|
112
|
+
export { AuthProvider } from './presentation/providers/AuthProvider';
|
|
113
|
+
|
|
108
114
|
// =============================================================================
|
|
109
115
|
// PRESENTATION LAYER - Hooks
|
|
110
116
|
// =============================================================================
|
|
@@ -112,6 +118,11 @@ export type {
|
|
|
112
118
|
export { useAuth } from './presentation/hooks/useAuth';
|
|
113
119
|
export type { UseAuthResult } from './presentation/hooks/useAuth';
|
|
114
120
|
|
|
121
|
+
export { useAuthRequired } from './presentation/hooks/useAuthRequired';
|
|
122
|
+
export type { UseAuthRequiredResult } from './presentation/hooks/useAuthRequired';
|
|
123
|
+
|
|
124
|
+
export { useRequireAuth, useUserId } from './presentation/hooks/useRequireAuth';
|
|
125
|
+
|
|
115
126
|
export { useUserProfile } from './presentation/hooks/useUserProfile';
|
|
116
127
|
export type { UserProfileData, UseUserProfileParams } from './presentation/hooks/useUserProfile';
|
|
117
128
|
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useAuthRequired Hook
|
|
3
|
+
* Check if user meets auth requirements for a feature
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* ```tsx
|
|
7
|
+
* const { isAllowed, requireAuth } = useAuthRequired();
|
|
8
|
+
*
|
|
9
|
+
* const handleAction = () => {
|
|
10
|
+
* if (!isAllowed) {
|
|
11
|
+
* requireAuth(); // Opens auth modal
|
|
12
|
+
* return;
|
|
13
|
+
* }
|
|
14
|
+
* // Proceed with action
|
|
15
|
+
* };
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { useCallback } from "react";
|
|
20
|
+
import { useAuthStore, selectIsAuthenticated } from "../stores/authStore";
|
|
21
|
+
import { useAuthModalStore } from "../stores/authModalStore";
|
|
22
|
+
|
|
23
|
+
export interface UseAuthRequiredResult {
|
|
24
|
+
/** Whether user is authenticated (not guest, not anonymous) */
|
|
25
|
+
isAllowed: boolean;
|
|
26
|
+
/** Whether auth is still loading */
|
|
27
|
+
isLoading: boolean;
|
|
28
|
+
/** Current user ID (null if not authenticated) */
|
|
29
|
+
userId: string | null;
|
|
30
|
+
/** Show auth modal to require authentication */
|
|
31
|
+
requireAuth: () => void;
|
|
32
|
+
/** Check and require auth - returns true if allowed, false if modal shown */
|
|
33
|
+
checkAndRequireAuth: () => boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Hook to check auth requirements and show modal if needed
|
|
38
|
+
*/
|
|
39
|
+
export function useAuthRequired(): UseAuthRequiredResult {
|
|
40
|
+
const isAllowed = useAuthStore(selectIsAuthenticated);
|
|
41
|
+
const isLoading = useAuthStore((state) => state.loading);
|
|
42
|
+
const userId = useAuthStore((state) => state.firebaseUser?.uid ?? null);
|
|
43
|
+
const showAuthModal = useAuthModalStore((state) => state.showAuthModal);
|
|
44
|
+
|
|
45
|
+
const requireAuth = useCallback(() => {
|
|
46
|
+
showAuthModal(undefined, "login");
|
|
47
|
+
}, [showAuthModal]);
|
|
48
|
+
|
|
49
|
+
const checkAndRequireAuth = useCallback((): boolean => {
|
|
50
|
+
if (isLoading) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!isAllowed) {
|
|
55
|
+
showAuthModal(undefined, "login");
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return true;
|
|
60
|
+
}, [isAllowed, isLoading, showAuthModal]);
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
isAllowed,
|
|
64
|
+
isLoading,
|
|
65
|
+
userId,
|
|
66
|
+
requireAuth,
|
|
67
|
+
checkAndRequireAuth,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useRequireAuth Hook
|
|
3
|
+
* Returns userId, throws if not authenticated
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* ```tsx
|
|
7
|
+
* // In a component that MUST have authenticated user
|
|
8
|
+
* const userId = useRequireAuth();
|
|
9
|
+
* // userId is guaranteed to be string, not null
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { useAuthStore, selectIsAuthenticated } from "../stores/authStore";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get userId or throw if not authenticated
|
|
17
|
+
* Use this in components that REQUIRE authentication
|
|
18
|
+
*/
|
|
19
|
+
export function useRequireAuth(): string {
|
|
20
|
+
const isAuthenticated = useAuthStore(selectIsAuthenticated);
|
|
21
|
+
const userId = useAuthStore((state) => state.firebaseUser?.uid ?? null);
|
|
22
|
+
|
|
23
|
+
if (!isAuthenticated || !userId) {
|
|
24
|
+
throw new Error("User not authenticated. This component requires auth.");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return userId;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get userId safely (returns null if not authenticated)
|
|
32
|
+
*/
|
|
33
|
+
export function useUserId(): string | null {
|
|
34
|
+
return useAuthStore((state) => state.firebaseUser?.uid ?? null);
|
|
35
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthProvider
|
|
3
|
+
* Wraps app and handles auth initialization
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* ```tsx
|
|
7
|
+
* <AuthProvider>
|
|
8
|
+
* <App />
|
|
9
|
+
* </AuthProvider>
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { useEffect, type ReactNode } from "react";
|
|
14
|
+
import { initializeAuthListener } from "../stores/authStore";
|
|
15
|
+
|
|
16
|
+
interface AuthProviderProps {
|
|
17
|
+
children: ReactNode;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* AuthProvider component
|
|
22
|
+
* Initializes Firebase auth listener on mount
|
|
23
|
+
* Must wrap the app root
|
|
24
|
+
*/
|
|
25
|
+
export function AuthProvider({ children }: AuthProviderProps): ReactNode {
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const unsubscribe = initializeAuthListener();
|
|
28
|
+
return unsubscribe;
|
|
29
|
+
}, []);
|
|
30
|
+
|
|
31
|
+
return <>{children}</>;
|
|
32
|
+
}
|