Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 34x 34x 8x 8x 4x 4x 16x 16x 16x 9x 9x 3x 3x 4x 4x 16x 3x 3x 19x 19x 19x 65x 65x 65x 115x 34x 34x 34x 34x | import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { AUTH_STORAGE_KEYS, AUTH_LOADING_STATES } from '@/constants/auth';
export interface User {
id: number;
username: string;
email: string;
confirmed: boolean;
blocked: boolean;
createdAt: string;
updatedAt: string;
}
export interface AuthState {
// State
user: User | null;
token: string | null;
isAuthenticated: boolean;
error: string | null;
// Loading states for different operations
isSigningIn: boolean;
isSigningUp: boolean;
isLoggingOut: boolean;
// Remember me functionality
rememberMe: boolean;
// Simplified actions
setError: (error: string | null) => void;
setRememberMe: (remember: boolean) => void;
setLoadingState: (
operation: 'signin' | 'signup' | 'logout',
loading: boolean
) => void;
login: (user: User, token: string) => void;
logout: () => void;
clearError: () => void;
}
export const useAuthStore = create<AuthState>()(
persist(
// Zustand store with persistence, Storing auth state in localStorage
(set, get) => ({
// Initial state
user: null,
token: null,
isAuthenticated: false,
error: null,
isSigningIn: false,
isSigningUp: false,
isLoggingOut: false,
rememberMe: false,
// Simplified actions
setError: (error: string | null) => {
console.log('🔐 [AuthStore] Setting error:', error);
set({ error });
},
setRememberMe: (remember: boolean) => {
console.log('🔐 [AuthStore] Setting remember me:', remember);
set({ rememberMe: remember });
},
setLoadingState: (
operation: 'signin' | 'signup' | 'logout',
loading: boolean
) => {
console.log(`🔐 [AuthStore] Setting ${operation} loading:`, loading);
const state: Partial<AuthState> = {};
switch (operation) {
case AUTH_LOADING_STATES.SIGNIN:
state.isSigningIn = loading;
break;
case AUTH_LOADING_STATES.SIGNUP:
state.isSigningUp = loading;
break;
case AUTH_LOADING_STATES.LOGOUT:
state.isLoggingOut = loading;
break;
}
set(state);
},
clearError: () => {
console.log('🔐 [AuthStore] Clearing error');
set({ error: null });
},
login: (user: User, token: string) => {
console.log('🔐 [AuthStore] Login action:', {
username: user.username,
email: user.email,
hasToken: !!token,
});
set({
user,
token,
isAuthenticated: true,
isSigningIn: false,
isSigningUp: false,
error: null,
});
console.log('🔐 [AuthStore] Login complete - state updated');
},
logout: () => {
console.log('🔐 [AuthStore] Logout action - clearing all auth data');
set({
user: null,
token: null,
isAuthenticated: false,
isSigningIn: false,
isSigningUp: false,
isLoggingOut: false,
error: null,
});
console.log('🔐 [AuthStore] Logout complete - state cleared');
},
}),
{
name: AUTH_STORAGE_KEYS.TOKEN, // This is the key used in localStorage
partialize: state => ({
// Selects which parts of the state to persist (user, token, and isAuthenticated)
user: state.user,
token: state.token,
isAuthenticated: state.isAuthenticated,
}),
onRehydrateStorage: () => state => {
// A callback that runs when the state is loaded from storage
console.log('🔐 [AuthStore] Rehydrating from localStorage');
if (state) {
console.log('🔐 [AuthStore] Rehydrated state:', {
hasUser: !!state.user,
hasToken: !!state.token,
isAuthenticated: state.isAuthenticated,
});
} else E{
console.log('🔐 [AuthStore] No stored state found');
}
},
}
)
);
|