fix login check and urls

This commit is contained in:
Patryk
2025-06-07 23:08:21 +02:00
parent 472dcfc96a
commit c19333ad8b
10 changed files with 424 additions and 374 deletions

View File

@@ -1,110 +1,135 @@
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";
const API_URL = "https://testowe.zikor.pl/api/v1";
const API_URL = "https://hopp.zikor.pl/api/v1";
export const useAuthStore = create(
persist(
(set) => ({
user: null,
token: null,
isLoading: false,
error: null,
persist(
(set, get) => {
if (!axios.interceptors.response.handlers.length) {
axios.interceptors.response.use(
(response) => response,
(error) => {
if (
(error.response && error.response.status === 401) ||
error.response.status === 403
) {
set({ user: null, token: null, isLoading: false });
delete axios.defaults.headers.common["Authorization"];
}
return Promise.reject(error);
}
);
}
signIn: async (email, password) => {
set({isLoading: true, error: null});
try {
const response = await axios.post(`${API_URL}/auth/login`, {
email,
password
});
return {
user: null,
token: null,
isLoading: false,
error: null,
const user = response.data.user;
const token = response.data.token;
set({user, token, isLoading: false});
} catch (error) {
set({error: error.response?.data?.message || error.message, isLoading: false});
throw error;
}
},
signIn: async (email, password) => {
set({ isLoading: true, error: null });
try {
const response = await axios.post(`${API_URL}/auth/login`, {
email,
password,
});
signUp: async (userData) => {
set({isLoading: true, error: null});
try {
console.log(userData);
const user = response.data.user;
const token = response.data.token;
set({ user, token, isLoading: false });
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
const response = await axios.post(`${API_URL}/auth/register`, userData, {
headers: {'Content-Type': 'application/json'}
});
signUp: async (userData) => {
set({ isLoading: true, error: null });
try {
const response = await axios.post(
`${API_URL}/auth/register`,
userData,
{
headers: { "Content-Type": "application/json" },
}
);
console.log(response.data);
const user = response.data.user;
const token = response.data.token;
set({ user, token, isLoading: false });
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
return user;
} catch (error) {
set({
error: error.response?.data?.message || error.message,
isLoading: false,
});
throw error;
}
},
const user = response.data.user;
const token = response.data.token;
set({user, token, isLoading: false});
signInWithGoogle: async (googleToken) => {
set({ isLoading: true, error: null });
try {
const response = await axios.post(`${API_URL}/auth/google`, {
token: googleToken,
});
return user;
} catch (error) {
set({error: error.response?.data?.message || error.message, isLoading: false});
throw error;
}
},
const { user, token } = response.data;
set({ user, token, isLoading: false });
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
return user;
} 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`, {token: googleToken});
signOut: async () => {
try {
const { token } = get();
const headers = token ? { Authorization: `Bearer ${token}` } : {};
await axios.post(`${API_URL}/auth/logout`, {}, { headers });
} catch (error) {
// console.error("Logout error:", error);
} finally {
delete axios.defaults.headers.common["Authorization"];
set({ user: null, token: null, isLoading: false });
}
},
const {user, token} = response.data;
set({user, token, isLoading: false});
checkAuth: async () => {
const { token } = get();
if (!token) return null;
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
return user;
} catch (error) {
set({error: error.response?.data?.message || error.message, isLoading: false});
throw error;
}
},
signOut: async () => {
try {
const {token} = useAuthStore.getState();
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
// Можно отправить запрос на бэкенд для инвалидации токена
await axios.post(`${API_URL}/auth/logout`, {}, { headers });
} catch (error) {
console.error("Logout error:", error);
} finally {
delete axios.defaults.headers.common["Authorization"];
set({user: 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: response.data, isLoading: false});
return response.data;
} catch (error) {
delete axios.defaults.headers.common["Authorization"];
set({user: null, token: null, isLoading: false});
return null;
}
},
}),
{
name: "auth-storage",
storage: createJSONStorage(() => AsyncStorage),
}
)
);
set({ isLoading: true });
try {
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
const response = await axios.get(`${API_URL}/auth/me`);
set({ user: response.data, isLoading: false });
return response.data;
} catch (error) {
delete axios.defaults.headers.common["Authorization"];
set({ user: null, token: null, isLoading: false });
return null;
}
},
};
},
{
name: "auth-storage",
storage: createJSONStorage(() => AsyncStorage),
}
)
);