Improve the inventory page
This commit is contained in:
@@ -58,13 +58,16 @@ export const useAuthStore = create<AuthState>()(
|
||||
login: async (email: string, password: string) => {
|
||||
try {
|
||||
set({ isLoading: true, error: null });
|
||||
|
||||
|
||||
const response = await authService.login({ email, password });
|
||||
|
||||
|
||||
if (response && response.access_token) {
|
||||
// Set the auth token on the API client immediately
|
||||
// Set the auth tokens on the API client immediately
|
||||
apiClient.setAuthToken(response.access_token);
|
||||
|
||||
if (response.refresh_token) {
|
||||
apiClient.setRefreshToken(response.refresh_token);
|
||||
}
|
||||
|
||||
set({
|
||||
user: response.user || null,
|
||||
token: response.access_token,
|
||||
@@ -92,13 +95,16 @@ export const useAuthStore = create<AuthState>()(
|
||||
register: async (userData: { email: string; password: string; full_name: string; tenant_name?: string }) => {
|
||||
try {
|
||||
set({ isLoading: true, error: null });
|
||||
|
||||
|
||||
const response = await authService.register(userData);
|
||||
|
||||
|
||||
if (response && response.access_token) {
|
||||
// Set the auth token on the API client immediately
|
||||
// Set the auth tokens on the API client immediately
|
||||
apiClient.setAuthToken(response.access_token);
|
||||
|
||||
if (response.refresh_token) {
|
||||
apiClient.setRefreshToken(response.refresh_token);
|
||||
}
|
||||
|
||||
set({
|
||||
user: response.user || null,
|
||||
token: response.access_token,
|
||||
@@ -124,10 +130,11 @@ export const useAuthStore = create<AuthState>()(
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
// Clear the auth token from API client
|
||||
// Clear the auth tokens from API client
|
||||
apiClient.setAuthToken(null);
|
||||
apiClient.setRefreshToken(null);
|
||||
apiClient.setTenantId(null);
|
||||
|
||||
|
||||
set({
|
||||
user: null,
|
||||
token: null,
|
||||
@@ -150,9 +157,12 @@ export const useAuthStore = create<AuthState>()(
|
||||
const response = await authService.refreshToken(refreshToken);
|
||||
|
||||
if (response && response.access_token) {
|
||||
// Set the auth token on the API client immediately
|
||||
// Set the auth tokens on the API client immediately
|
||||
apiClient.setAuthToken(response.access_token);
|
||||
|
||||
if (response.refresh_token) {
|
||||
apiClient.setRefreshToken(response.refresh_token);
|
||||
}
|
||||
|
||||
set({
|
||||
token: response.access_token,
|
||||
refreshToken: response.refresh_token || refreshToken,
|
||||
@@ -244,11 +254,14 @@ export const useAuthStore = create<AuthState>()(
|
||||
isAuthenticated: state.isAuthenticated,
|
||||
}),
|
||||
onRehydrateStorage: () => (state) => {
|
||||
// Initialize API client with stored token when store rehydrates
|
||||
// Initialize API client with stored tokens when store rehydrates
|
||||
if (state?.token) {
|
||||
import('../api').then(({ apiClient }) => {
|
||||
apiClient.setAuthToken(state.token!);
|
||||
|
||||
if (state.refreshToken) {
|
||||
apiClient.setRefreshToken(state.refreshToken);
|
||||
}
|
||||
|
||||
if (state.user?.tenant_id) {
|
||||
apiClient.setTenantId(state.user.tenant_id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user