@umituz/react-native-auth 3.2.6 → 3.2.8

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.2.6",
3
+ "version": "3.2.8",
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",
@@ -9,6 +9,13 @@
9
9
  import { useCallback } from "react";
10
10
  import {
11
11
  useAuthStore,
12
+ selectUser,
13
+ selectLoading,
14
+ selectIsGuest,
15
+ selectError,
16
+ selectSetLoading,
17
+ selectSetError,
18
+ selectSetIsGuest,
12
19
  selectIsAuthenticated,
13
20
  selectUserId,
14
21
  selectUserType,
@@ -62,21 +69,21 @@ export interface UseAuthResult {
62
69
  * Must call initializeAuthListener() once in app root.
63
70
  */
64
71
  export function useAuth(): UseAuthResult {
65
- // State from store
66
- const user = useAuthStore((state) => state.user);
67
- const loading = useAuthStore((state) => state.loading);
68
- const isGuest = useAuthStore((state) => state.isGuest);
69
- const error = useAuthStore((state) => state.error);
72
+ // State from store - using typed selectors
73
+ const user = useAuthStore(selectUser);
74
+ const loading = useAuthStore(selectLoading);
75
+ const isGuest = useAuthStore(selectIsGuest);
76
+ const error = useAuthStore(selectError);
70
77
  const isAuthenticated = useAuthStore(selectIsAuthenticated);
71
78
  const userId = useAuthStore(selectUserId);
72
79
  const userType = useAuthStore(selectUserType);
73
80
  const isAnonymous = useAuthStore(selectIsAnonymous);
74
81
  const isAuthReady = useAuthStore(selectIsAuthReady);
75
82
 
76
- // Actions from store
77
- const setLoading = useAuthStore((state) => state.setLoading);
78
- const setError = useAuthStore((state) => state.setError);
79
- const setIsGuest = useAuthStore((state) => state.setIsGuest);
83
+ // Actions from store - using typed selectors
84
+ const setLoading = useAuthStore(selectSetLoading);
85
+ const setError = useAuthStore(selectSetError);
86
+ const setIsGuest = useAuthStore(selectSetIsGuest);
80
87
 
81
88
  // Mutations
82
89
  const signInMutation = useSignInMutation();
@@ -17,8 +17,9 @@
17
17
  */
18
18
 
19
19
  import { useCallback } from "react";
20
- import { useAuthStore, selectIsAuthenticated } from "../stores/authStore";
20
+ import { useAuthStore, selectIsAuthenticated, selectLoading, selectFirebaseUserId } from "../stores/authStore";
21
21
  import { useAuthModalStore } from "../stores/authModalStore";
22
+ import { selectShowAuthModal } from "../stores/auth.selectors";
22
23
 
23
24
  export interface UseAuthRequiredResult {
24
25
  /** Whether user is authenticated (not guest, not anonymous) */
@@ -38,9 +39,9 @@ export interface UseAuthRequiredResult {
38
39
  */
39
40
  export function useAuthRequired(): UseAuthRequiredResult {
40
41
  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);
42
+ const isLoading = useAuthStore(selectLoading);
43
+ const userId = useAuthStore(selectFirebaseUserId);
44
+ const showAuthModal = useAuthModalStore(selectShowAuthModal);
44
45
 
45
46
  const requireAuth = useCallback(() => {
46
47
  showAuthModal(undefined, "login");
@@ -10,7 +10,7 @@
10
10
  * ```
11
11
  */
12
12
 
13
- import { useAuthStore, selectIsAuthenticated } from "../stores/authStore";
13
+ import { useAuthStore, selectIsAuthenticated, selectFirebaseUserId } from "../stores/authStore";
14
14
 
15
15
  /**
16
16
  * Get userId or throw if not authenticated
@@ -18,7 +18,7 @@ import { useAuthStore, selectIsAuthenticated } from "../stores/authStore";
18
18
  */
19
19
  export function useRequireAuth(): string {
20
20
  const isAuthenticated = useAuthStore(selectIsAuthenticated);
21
- const userId = useAuthStore((state) => state.firebaseUser?.uid ?? null);
21
+ const userId = useAuthStore(selectFirebaseUserId);
22
22
 
23
23
  if (!isAuthenticated || !userId) {
24
24
  throw new Error("User not authenticated. This component requires auth.");
@@ -31,5 +31,5 @@ export function useRequireAuth(): string {
31
31
  * Get userId safely (returns null if not authenticated)
32
32
  */
33
33
  export function useUserId(): string | null {
34
- return useAuthStore((state) => state.firebaseUser?.uid ?? null);
34
+ return useAuthStore(selectFirebaseUserId);
35
35
  }
@@ -3,33 +3,97 @@
3
3
  * Pure functions for deriving state from auth store
4
4
  */
5
5
 
6
- import type { AuthState, UserType } from "../../types/auth-store.types";
6
+ import type { AuthState, AuthActions, UserType } from "../../types/auth-store.types";
7
+ import type { AuthUser } from "../../domain/entities/AuthUser";
8
+
9
+ // Combined store type for selectors
10
+ type AuthStore = AuthState & AuthActions;
11
+
12
+ // =============================================================================
13
+ // STATE SELECTORS
14
+ // =============================================================================
15
+
16
+ /**
17
+ * Select current user
18
+ */
19
+ export const selectUser = (state: AuthStore): AuthUser | null => state.user;
20
+
21
+ /**
22
+ * Select loading state
23
+ */
24
+ export const selectLoading = (state: AuthStore): boolean => state.loading;
25
+
26
+ /**
27
+ * Select guest mode
28
+ */
29
+ export const selectIsGuest = (state: AuthStore): boolean => state.isGuest;
30
+
31
+ /**
32
+ * Select error
33
+ */
34
+ export const selectError = (state: AuthStore): string | null => state.error;
35
+
36
+ /**
37
+ * Select firebase user uid
38
+ */
39
+ export const selectFirebaseUserId = (state: AuthStore): string | null =>
40
+ state.firebaseUser?.uid ?? null;
41
+
42
+ // =============================================================================
43
+ // ACTION SELECTORS
44
+ // =============================================================================
45
+
46
+ /**
47
+ * Select setLoading action
48
+ */
49
+ export const selectSetLoading = (state: AuthStore) => state.setLoading;
50
+
51
+ /**
52
+ * Select setError action
53
+ */
54
+ export const selectSetError = (state: AuthStore) => state.setError;
55
+
56
+ /**
57
+ * Select setIsGuest action
58
+ */
59
+ export const selectSetIsGuest = (state: AuthStore) => state.setIsGuest;
60
+
61
+ /**
62
+ * Select showAuthModal action (from authModalStore)
63
+ */
64
+ export type AuthModalMode = "login" | "register";
65
+ export const selectShowAuthModal = (state: { showAuthModal: (callback?: () => void | Promise<void>, mode?: AuthModalMode) => void }) =>
66
+ state.showAuthModal;
67
+
68
+ // =============================================================================
69
+ // DERIVED SELECTORS
70
+ // =============================================================================
7
71
 
8
72
  /**
9
73
  * Get current user ID
10
74
  */
11
- export const selectUserId = (state: AuthState): string | null => {
75
+ export const selectUserId = (state: AuthStore): string | null => {
12
76
  return state.firebaseUser?.uid ?? state.user?.uid ?? null;
13
77
  };
14
78
 
15
79
  /**
16
80
  * Check if user is authenticated (not guest, not anonymous)
17
81
  */
18
- export const selectIsAuthenticated = (state: AuthState): boolean => {
82
+ export const selectIsAuthenticated = (state: AuthStore): boolean => {
19
83
  return !!state.user && !state.isGuest && !state.user.isAnonymous;
20
84
  };
21
85
 
22
86
  /**
23
87
  * Check if user is anonymous
24
88
  */
25
- export const selectIsAnonymous = (state: AuthState): boolean => {
89
+ export const selectIsAnonymous = (state: AuthStore): boolean => {
26
90
  return state.firebaseUser?.isAnonymous ?? state.user?.isAnonymous ?? false;
27
91
  };
28
92
 
29
93
  /**
30
94
  * Get current user type
31
95
  */
32
- export const selectUserType = (state: AuthState): UserType => {
96
+ export const selectUserType = (state: AuthStore): UserType => {
33
97
  if (!state.firebaseUser && !state.user) {
34
98
  return "none";
35
99
  }
@@ -43,6 +107,6 @@ export const selectUserType = (state: AuthState): UserType => {
43
107
  /**
44
108
  * Check if auth is ready (initialized and not loading)
45
109
  */
46
- export const selectIsAuthReady = (state: AuthState): boolean => {
110
+ export const selectIsAuthReady = (state: AuthStore): boolean => {
47
111
  return state.initialized && !state.loading;
48
112
  };
@@ -17,7 +17,7 @@
17
17
  * ```
18
18
  */
19
19
 
20
- import { create } from "zustand";
20
+ import { create, type StoreApi, type UseBoundStore } from "zustand";
21
21
 
22
22
  export type AuthModalMode = "login" | "register";
23
23
 
@@ -40,12 +40,21 @@ interface AuthModalActions {
40
40
 
41
41
  type AuthModalStore = AuthModalState & AuthModalActions;
42
42
 
43
- export const useAuthModalStore = create<AuthModalStore>((set, get) => ({
43
+ type SetModalState = (partial: Partial<AuthModalState> | ((state: AuthModalState) => Partial<AuthModalState>)) => void;
44
+ type GetModalState = () => AuthModalStore;
45
+
46
+ export const useAuthModalStore: UseBoundStore<StoreApi<AuthModalStore>> = create<AuthModalStore>((
47
+ set: SetModalState,
48
+ get: GetModalState,
49
+ ) => ({
44
50
  isVisible: false,
45
51
  mode: "login",
46
52
  pendingCallback: null,
47
53
 
48
- showAuthModal: (callback, mode = "login" as AuthModalMode) => {
54
+ showAuthModal: (
55
+ callback?: () => void | Promise<void>,
56
+ mode: AuthModalMode = "login",
57
+ ) => {
49
58
  set({
50
59
  isVisible: true,
51
60
  mode,
@@ -57,7 +66,7 @@ export const useAuthModalStore = create<AuthModalStore>((set, get) => ({
57
66
  set({ isVisible: false });
58
67
  },
59
68
 
60
- setMode: (mode) => {
69
+ setMode: (mode: AuthModalMode) => {
61
70
  set({ mode });
62
71
  },
63
72
 
@@ -69,7 +78,6 @@ export const useAuthModalStore = create<AuthModalStore>((set, get) => ({
69
78
  }
70
79
  },
71
80
 
72
-
73
81
  clearPendingCallback: () => {
74
82
  set({ pendingCallback: null });
75
83
  },
@@ -7,11 +7,29 @@
7
7
  */
8
8
 
9
9
  import { createStore } from "@umituz/react-native-storage";
10
+ import type { User } from "firebase/auth";
10
11
  import type { AuthUser } from "../../domain/entities/AuthUser";
11
12
  import { mapToAuthUser } from "../../infrastructure/utils/UserMapper";
12
13
  import type { AuthState, AuthActions, UserType } from "../../types/auth-store.types";
13
14
  import { initialAuthState } from "../../types/auth-store.types";
15
+
16
+ // Type for the combined store
17
+ type AuthStore = AuthState & AuthActions;
18
+
19
+ // Type for set function
20
+ type SetState = (partial: Partial<AuthState>) => void;
21
+
22
+ // Type for get function
23
+ type GetState = () => AuthStore;
14
24
  import {
25
+ selectUser,
26
+ selectLoading,
27
+ selectIsGuest,
28
+ selectError,
29
+ selectFirebaseUserId,
30
+ selectSetLoading,
31
+ selectSetError,
32
+ selectSetIsGuest,
15
33
  selectUserId,
16
34
  selectIsAuthenticated,
17
35
  selectIsAnonymous,
@@ -24,6 +42,14 @@ export type { AuthState, AuthActions, UserType };
24
42
 
25
43
  // Re-export selectors
26
44
  export {
45
+ selectUser,
46
+ selectLoading,
47
+ selectIsGuest,
48
+ selectError,
49
+ selectFirebaseUserId,
50
+ selectSetLoading,
51
+ selectSetError,
52
+ selectSetIsGuest,
27
53
  selectUserId,
28
54
  selectIsAuthenticated,
29
55
  selectIsAnonymous,
@@ -47,12 +73,12 @@ export const useAuthStore = createStore<AuthState, AuthActions>({
47
73
  initialState: initialAuthState,
48
74
  persist: true,
49
75
  version: 1,
50
- partialize: (state) => ({
76
+ partialize: (state: AuthState) => ({
51
77
  isGuest: state.isGuest,
52
78
  initialized: state.initialized,
53
79
  }),
54
- actions: (set, get) => ({
55
- setFirebaseUser: (firebaseUser) => {
80
+ actions: (set: SetState, get: GetState) => ({
81
+ setFirebaseUser: (firebaseUser: User | null) => {
56
82
  const { isGuest } = get();
57
83
 
58
84
  let user: AuthUser | null = null;
@@ -72,9 +98,9 @@ export const useAuthStore = createStore<AuthState, AuthActions>({
72
98
  });
73
99
  },
74
100
 
75
- setLoading: (loading) => set({ loading }),
101
+ setLoading: (loading: boolean) => set({ loading }),
76
102
 
77
- setIsGuest: (isGuest) => {
103
+ setIsGuest: (isGuest: boolean) => {
78
104
  const { firebaseUser } = get();
79
105
 
80
106
  let user: AuthUser | null = null;
@@ -89,9 +115,9 @@ export const useAuthStore = createStore<AuthState, AuthActions>({
89
115
  set({ isGuest, user });
90
116
  },
91
117
 
92
- setError: (error) => set({ error }),
118
+ setError: (error: string | null) => set({ error }),
93
119
 
94
- setInitialized: (initialized) => set({ initialized }),
120
+ setInitialized: (initialized: boolean) => set({ initialized }),
95
121
 
96
122
  reset: () => set(initialAuthState),
97
123
  }),