some fixes

This commit is contained in:
Patryk
2025-06-09 20:59:43 +02:00
parent 77c3a694f8
commit 27175ffa91
7 changed files with 221 additions and 197 deletions

View File

@@ -1,5 +1,5 @@
import {create} from "zustand";
import {createJSONStorage, persist} from "zustand/middleware";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import AsyncStorage from "@react-native-async-storage/async-storage";
import axios from "axios";
@@ -8,135 +8,135 @@ const API_URL = "https://hopp.zikor.pl/api/v1";
let interceptorInitialized = false;
export const useAuthStore = create(
persist(
(set, get) => {
if (!interceptorInitialized.current) {
axios.interceptors.response.use(
(response) => response,
(error) => {
if (
(error.response && error.response.status === 401) ||
error.response.status === 403
) {
set({user_id: null, token: null, isLoading: false});
delete axios.defaults.headers.common["Authorization"];
}
return Promise.reject(error);
}
);
interceptorInitialized = true;
}
return {
user_id: null,
token: null,
isLoading: false,
error: null,
persist(
(set, get) => {
// if (!interceptorInitialized.current) {
// axios.interceptors.response.use(
// (response) => response,
// (error) => {
// if (
// (error.response && error.response.status === 401) ||
// error.response.status === 403
// ) {
// set({ user_id: null, token: null, isLoading: false });
// delete axios.defaults.headers.common["Authorization"];
// }
// return Promise.reject(error);
// }
// );
// interceptorInitialized = true;
// }
return {
user_id: null,
token: null,
isLoading: false,
error: null,
signIn: async (email, password) => {
set({isLoading: true, error: null});
try {
const response = await axios.post(`${API_URL}/auth/login`, {
email,
password,
});
signIn: async (email, password) => {
set({ isLoading: true, error: null });
try {
const response = await axios.post(`${API_URL}/auth/login`, {
email,
password,
});
const user_id = response.data.user_id;
const token = response.data.token;
set({user_id: user_id, token: token, isLoading: false});
const user_id = response.data.user_id;
const token = response.data.token;
set({ user_id: user_id, token: token, isLoading: false });
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
signUp: async (userData) => {
set({isLoading: true, error: null});
try {
const response = await axios.post(
`${API_URL}/auth/register`,
userData,
{
headers: {"Content-Type": "application/json"},
}
);
const user_id = response.data.user_id;
const token = response.data.token;
set({user_id: user_id, token: token, isLoading: false});
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
signInWithGoogle: async (googleToken) => {
set({isLoading: true, error: null});
try {
const response = await axios.post(
`${API_URL}/auth/google`,
{googleToken: googleToken},
{
headers: {"Content-Type": "application/json"},
}
);
const user_id = response.data.user_id;
const token = response.data.token;
set({user_id: user_id, token: token, isLoading: false});
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
signOut: async () => {
try {
await axios.post(`${API_URL}/auth/logout`);
} catch (error) {
console.error("Logout error:", error);
} finally {
delete axios.defaults.headers.common["Authorization"];
set({user_id: null, token: null});
}
},
// checkAuth: async () => {
// const { token } = useAuthStore.getState();
// if (!token) return null;
// set({ isLoading: true });
// try {
// axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
// const response = await axios.get(`${API_URL}/auth/me`);
// set({ user_id: response.data, isLoading: false });
// return response.data;
// } catch (error) {
// delete axios.defaults.headers.common["Authorization"];
// set({ user_id: null, token: null, isLoading: false });
// return null;
// }
// },
};
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
{
name: "auth-storage",
storage: createJSONStorage(() => AsyncStorage),
}
)
signUp: async (userData) => {
set({ isLoading: true, error: null });
try {
const response = await axios.post(
`${API_URL}/auth/register`,
userData,
{
headers: { "Content-Type": "application/json" },
}
);
const user_id = response.data.user_id;
const token = response.data.token;
set({ user_id: user_id, token: token, isLoading: false });
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
signInWithGoogle: async (googleToken) => {
set({ isLoading: true, error: null });
try {
const response = await axios.post(
`${API_URL}/auth/google`,
{ googleToken: googleToken },
{
headers: { "Content-Type": "application/json" },
}
);
const user_id = response.data.user_id;
const token = response.data.token;
set({ user_id: user_id, token: token, isLoading: false });
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
signOut: async () => {
try {
await axios.post(`${API_URL}/auth/logout`);
} catch (error) {
console.error("Logout error:", error);
} finally {
delete axios.defaults.headers.common["Authorization"];
set({ user_id: null, token: null });
}
},
// checkAuth: async () => {
// const { token } = useAuthStore.getState();
// if (!token) return null;
// set({ isLoading: true });
// try {
// axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
// const response = await axios.get(`${API_URL}/auth/me`);
// set({ user_id: response.data, isLoading: false });
// return response.data;
// } catch (error) {
// delete axios.defaults.headers.common["Authorization"];
// set({ user_id: null, token: null, isLoading: false });
// return null;
// }
// },
};
},
{
name: "auth-storage",
storage: createJSONStorage(() => AsyncStorage),
}
)
);