fix login check and urls
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import axios from "axios";
|
||||
import {useAuthStore} from "@/store/authStore";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
const API_URL = "https://testowe.zikor.pl/api/v1";
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
export async function listCategories() {
|
||||
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/vars/categories`, { headers });
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import axios from "axios";
|
||||
|
||||
const API_URL = "https://testowe.zikor.pl/api/v1";
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
export async function getUserById(userId) {
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/clients/get/${userId}`);
|
||||
return response.data;
|
||||
} catch (err) {
|
||||
console.error(`Nie udało się pobrać danych użytkownika o ID ${userId}.`, err.response.status);
|
||||
console.error(
|
||||
`Nie udało się pobrać danych użytkownika o ID ${userId}.`,
|
||||
err.response.status
|
||||
);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
import axios from "axios";
|
||||
import FormData from 'form-data'
|
||||
import {useAuthStore} from "@/store/authStore";
|
||||
import FormData from "form-data";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
const API_URL = "https://testowe.zikor.pl/api/v1";
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
//const API_URL = "http://10.0.2.2:8080/api/v1";
|
||||
|
||||
export async function listNotices() {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
console.log(token);
|
||||
|
||||
const response = await fetch(`${API_URL}/notices/get/all`, {
|
||||
headers: headers
|
||||
headers: headers,
|
||||
});
|
||||
console.log(response);
|
||||
// console.log(response);r
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(response.toString());
|
||||
@@ -77,8 +77,8 @@ export async function getAllImagesByNoticeId(noticeId) {
|
||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
||||
|
||||
if (listResponse.data && listResponse.data.length > 0) {
|
||||
const imageUrls = listResponse.data.map(imageName =>
|
||||
`${API_URL}/images/get/${imageName}`
|
||||
const imageUrls = listResponse.data.map(
|
||||
(imageName) => `${API_URL}/images/get/${imageName}`
|
||||
);
|
||||
|
||||
// console.log(`Pobrano ${imageUrls.length} zdjęć dla ogłoszenia o id: ${noticeId}`);
|
||||
@@ -98,12 +98,12 @@ export const uploadImage = async (noticeId, imageUri) => {
|
||||
|
||||
const formData = new FormData();
|
||||
|
||||
const filename = imageUri.split('/').pop();
|
||||
const filename = imageUri.split("/").pop();
|
||||
|
||||
const match = /\.(\w+)$/.exec(filename);
|
||||
const type = match ? `image/${match[1]}` : 'image/jpeg';
|
||||
const type = match ? `image/${match[1]}` : "image/jpeg";
|
||||
|
||||
formData.append('file', {
|
||||
formData.append("file", {
|
||||
uri: imageUri,
|
||||
name: filename,
|
||||
type: type,
|
||||
@@ -115,15 +115,19 @@ export const uploadImage = async (noticeId, imageUri) => {
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
}
|
||||
);
|
||||
console.info('Upload successful:', response.data);
|
||||
console.info("Upload successful:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log("imageURI:", imageUri);
|
||||
console.error('Error uploading image:', error.response.data, error.response.status);
|
||||
console.error(
|
||||
"Error uploading image:",
|
||||
error.response.data,
|
||||
error.response.status
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
import axios from "axios";
|
||||
import {useAuthStore} from "@/store/authStore";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
// import FormData from 'form-data'
|
||||
|
||||
const API_URL = "https://testowe.zikor.pl/api/v1/wishlist";
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1/wishlist";
|
||||
|
||||
export async function toggleNoticeStatus(noticeId) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/toggle/${noticeId}`, {}, {
|
||||
headers
|
||||
});
|
||||
const response = await axios.post(
|
||||
`${API_URL}/toggle/${noticeId}`,
|
||||
{},
|
||||
{
|
||||
headers,
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Error toggling wishlist item:", error);
|
||||
@@ -21,10 +25,10 @@ export async function toggleNoticeStatus(noticeId) {
|
||||
|
||||
export async function getWishlist() {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/`, {headers});
|
||||
const response = await axios.get(`${API_URL}/`, { headers });
|
||||
console.log("Wishlist response:", response.data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import {Tabs} from "expo-router";
|
||||
import {Ionicons} from "@expo/vector-icons";
|
||||
import { Tabs, Redirect } from "expo-router";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
export default function TabLayout() {
|
||||
const token = useAuthStore((state) => state.token);
|
||||
|
||||
if (!token) {
|
||||
return <Redirect href="/login" />;
|
||||
}
|
||||
return (
|
||||
<Tabs
|
||||
screenOptions={{
|
||||
@@ -13,8 +19,8 @@ export default function TabLayout() {
|
||||
options={{
|
||||
title: "Home",
|
||||
tabBarLabel: "Home",
|
||||
tabBarIcon: ({color, size}) => (
|
||||
<Ionicons name="home-outline" size={size} color={color}/>
|
||||
tabBarIcon: ({ color, size }) => (
|
||||
<Ionicons name="home-outline" size={size} color={color} />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
@@ -23,8 +29,8 @@ export default function TabLayout() {
|
||||
options={{
|
||||
title: "Ogłoszenia",
|
||||
tabBarLabel: "Ogłoszenia",
|
||||
tabBarIcon: ({color, size}) => (
|
||||
<Ionicons name="list-outline" size={size} color={color}/>
|
||||
tabBarIcon: ({ color, size }) => (
|
||||
<Ionicons name="list-outline" size={size} color={color} />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
@@ -33,8 +39,8 @@ export default function TabLayout() {
|
||||
options={{
|
||||
title: "Dodaj",
|
||||
tabBarLabel: "Dodaj",
|
||||
tabBarIcon: ({color, size}) => (
|
||||
<Ionicons name="add-circle-outline" size={size} color={color}/>
|
||||
tabBarIcon: ({ color, size }) => (
|
||||
<Ionicons name="add-circle-outline" size={size} color={color} />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
@@ -43,8 +49,8 @@ export default function TabLayout() {
|
||||
options={{
|
||||
title: "Ulubione",
|
||||
tabBarLabel: "Ulubione",
|
||||
tabBarIcon: ({color, size}) => (
|
||||
<Ionicons name="heart-outline" size={size} color={color}/>
|
||||
tabBarIcon: ({ color, size }) => (
|
||||
<Ionicons name="heart-outline" size={size} color={color} />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
@@ -54,8 +60,8 @@ export default function TabLayout() {
|
||||
headerShown: false,
|
||||
title: "Konto",
|
||||
tabBarLabel: "Konto",
|
||||
tabBarIcon: ({color, size}) => (
|
||||
<Ionicons name="person-outline" size={size} color={color}/>
|
||||
tabBarIcon: ({ color, size }) => (
|
||||
<Ionicons name="person-outline" size={size} color={color} />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import { DrawerItem } from "@react-navigation/drawer";
|
||||
import { Drawer } from "expo-router/drawer";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
import {
|
||||
DrawerContentScrollView,
|
||||
DrawerItemList,
|
||||
} from "@react-navigation/drawer";
|
||||
|
||||
export default function AccountDrawerLayout() {
|
||||
const signOut = useAuthStore((state) => state.signOut);
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
screenOptions={{
|
||||
@@ -9,10 +18,19 @@ export default function AccountDrawerLayout() {
|
||||
drawerActiveBackgroundColor: "#f0f0f0",
|
||||
drawerItemStyle: {
|
||||
borderRadius: 8,
|
||||
// backgroundColor: "transparent",
|
||||
},
|
||||
headerTintColor: "#1c1c1e",
|
||||
}}
|
||||
drawerContent={(props) => (
|
||||
<DrawerContentScrollView {...props}>
|
||||
<DrawerItemList {...props} />
|
||||
<DrawerItem
|
||||
label="Wyloguj"
|
||||
onPress={signOut}
|
||||
labelStyle={{ color: "red" }}
|
||||
/>
|
||||
</DrawerContentScrollView>
|
||||
)}
|
||||
>
|
||||
<Drawer.Screen name="account" options={{ title: "Konto" }} />
|
||||
<Drawer.Screen
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import { ScrollView, View } from "react-native";
|
||||
import { useNoticesStore } from '@/store/noticesStore';
|
||||
import { useNoticesStore } from "@/store/noticesStore";
|
||||
import { CategorySection } from "@/components/CategorySection";
|
||||
import { NoticeSection } from "@/components/NoticeSection";
|
||||
import { UserSection } from "@/components/UserSection";
|
||||
import { SearchSection } from "@/components/SearchSection";
|
||||
import { FlatList } from 'react-native';
|
||||
import { FlatList } from "react-native";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
import { useRouter } from "expo-router";
|
||||
import { useEffect, useState } from "react";
|
||||
// import { SafeAreaView } from "react-native-safe-area-context";
|
||||
import { SafeAreaView } from "react-native";
|
||||
|
||||
export default function Home() {
|
||||
const token = useAuthStore((state) => state.token);
|
||||
const token = useAuthStore((state) => state.token);
|
||||
const router = useRouter();
|
||||
const [isReady, setIsReady] = useState(false);
|
||||
const fetchNotices = useNoticesStore((state) => state.fetchNotices);
|
||||
@@ -27,13 +26,11 @@ const token = useAuthStore((state) => state.token);
|
||||
}
|
||||
}, [isReady, token, router]);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (token) {
|
||||
fetchNotices();
|
||||
}
|
||||
}, [token, fetchNotices]);
|
||||
|
||||
}, [token, fetchNotices]);
|
||||
|
||||
const notices = useNoticesStore((state) => state.notices);
|
||||
// console.log("Notices:", notices);
|
||||
@@ -45,16 +42,23 @@ const token = useAuthStore((state) => state.token);
|
||||
.sort(() => Math.random() - 0.5)
|
||||
.slice(0, 6);
|
||||
|
||||
|
||||
return (
|
||||
<SafeAreaView className="flex-1 m-2">
|
||||
{/* <View> */}
|
||||
<SearchSection/>
|
||||
<ScrollView showsVerticalScrollIndicator={false} >
|
||||
<SearchSection />
|
||||
<ScrollView showsVerticalScrollIndicator={false}>
|
||||
<CategorySection title="Polecane kategorie" notices={notices} />
|
||||
<NoticeSection title="Najnowsze ogłoszenia" notices={latestNotices} ctaLink="/notices?sort=latest"/>
|
||||
<NoticeSection
|
||||
title="Najnowsze ogłoszenia"
|
||||
notices={latestNotices}
|
||||
ctaLink="/notices?sort=latest"
|
||||
/>
|
||||
<UserSection title="Popularni sprzedawcy" notices={notices} />
|
||||
<NoticeSection title="Proponowane ogłoszenia" notices={recomendedNotices} ctaLink="/notices"/>
|
||||
<NoticeSection
|
||||
title="Proponowane ogłoszenia"
|
||||
notices={recomendedNotices}
|
||||
ctaLink="/notices"
|
||||
/>
|
||||
</ScrollView>
|
||||
{/* </View> */}
|
||||
</SafeAreaView>
|
||||
|
||||
@@ -1,32 +1,25 @@
|
||||
import { View, FlatList} from 'react-native';
|
||||
import { useEffect, useState } from 'react'
|
||||
import { View, FlatList } from "react-native";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
import { Heading } from '@/components/ui/heading';
|
||||
import { Text } from '@/components/ui/text';
|
||||
import { Link } from 'expo-router';
|
||||
import { Pressable } from '@/components/ui/pressable';
|
||||
// import axios from 'axios';
|
||||
import {listCategories} from "@/api/categories";
|
||||
|
||||
|
||||
export function CategorySection({notices, title}) {
|
||||
const token = useAuthStore((state) => state.token);
|
||||
import { Heading } from "@/components/ui/heading";
|
||||
import { Text } from "@/components/ui/text";
|
||||
import { Link } from "expo-router";
|
||||
import { Pressable } from "@/components/ui/pressable";
|
||||
import { listCategories } from "@/api/categories";
|
||||
|
||||
export function CategorySection({ notices, title }) {
|
||||
const [categoryMap, setCategoryMap] = useState({});
|
||||
|
||||
useEffect(() => {
|
||||
if(token){
|
||||
const fetchCategories = async () => {
|
||||
let data = await listCategories();
|
||||
if (Array.isArray(data)) {
|
||||
setCategoryMap(data);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fetchCategories();
|
||||
}
|
||||
}, [token]);
|
||||
|
||||
});
|
||||
|
||||
const categories = Array.from(
|
||||
new Set(notices.map((notice) => notice.category))
|
||||
@@ -35,13 +28,11 @@ export function CategorySection({notices, title}) {
|
||||
const getCount = (category) =>
|
||||
notices.filter((notice) => notice.category === category).length;
|
||||
|
||||
console.log("CategoryMap:", categoryMap);
|
||||
|
||||
if(!categoryMap) {
|
||||
if (!categoryMap || Object.keys(categoryMap).length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
return (
|
||||
<View className="mb-6">
|
||||
<Heading className="text-2xl font-bold mb-4 mt-4">{title}</Heading>
|
||||
<FlatList
|
||||
@@ -65,5 +56,4 @@ return (
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
|
||||
}
|
||||
@@ -1,27 +1,27 @@
|
||||
import { View} from 'react-native';
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Heading } from '@/components/ui/heading';
|
||||
import { FlatList } from 'react-native';
|
||||
import axios from 'axios';
|
||||
import UserBlock from '@/components/UserBlock';
|
||||
import {useAuthStore} from "@/store/authStore";
|
||||
import { View } from "react-native";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Heading } from "@/components/ui/heading";
|
||||
import { FlatList } from "react-native";
|
||||
import axios from "axios";
|
||||
import UserBlock from "@/components/UserBlock";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
|
||||
|
||||
export function UserSection({notices, title}) {
|
||||
export function UserSection({ notices, title }) {
|
||||
const token = useAuthStore((state) => state.token);
|
||||
const headers = token ? { 'Authorization': `Bearer ${token}` } : {};
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
const [users, setUsers] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (token){
|
||||
axios.get('https://testowe.zikor.pl/api/v1/clients/get/all', { headers })
|
||||
.then(res => setUsers(res.data))
|
||||
if (token) {
|
||||
axios
|
||||
.get("https://hopp.zikor.pl/api/v1/clients/get/all", { headers })
|
||||
.then((res) => setUsers(res.data))
|
||||
.catch(() => setUsers([]));
|
||||
}
|
||||
}, [token]);
|
||||
|
||||
const usersWithNoticeCount = users.map(user => {
|
||||
const count = notices.filter(n => n.clientId === user.id).length;
|
||||
const usersWithNoticeCount = users.map((user) => {
|
||||
const count = notices.filter((n) => n.clientId === user.id).length;
|
||||
return { ...user, noticeCount: count };
|
||||
});
|
||||
|
||||
@@ -29,7 +29,7 @@ export function UserSection({notices, title}) {
|
||||
.sort((a, b) => b.noticeCount - a.noticeCount)
|
||||
.slice(0, 5);
|
||||
|
||||
return (
|
||||
return (
|
||||
<View className="mb-6">
|
||||
<Heading className="text-2xl font-bold mb-4 mt-4">{title}</Heading>
|
||||
<FlatList
|
||||
@@ -38,12 +38,9 @@ return (
|
||||
showsHorizontalScrollIndicator={false}
|
||||
contentContainerStyle={{ paddingHorizontal: 8, gap: 12 }}
|
||||
renderItem={({ item }) => {
|
||||
return (
|
||||
<UserBlock user={item} />
|
||||
);
|
||||
return <UserBlock user={item} />;
|
||||
}}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
|
||||
}
|
||||
@@ -1,107 +1,132 @@
|
||||
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) => ({
|
||||
(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);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
user: null,
|
||||
token: null,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
|
||||
signIn: async (email, password) => {
|
||||
set({isLoading: true, error: null});
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/auth/login`, {
|
||||
email,
|
||||
password
|
||||
password,
|
||||
});
|
||||
|
||||
const user = response.data.user;
|
||||
const token = response.data.token;
|
||||
set({user, token, isLoading: false});
|
||||
set({ user, token, isLoading: false });
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
} catch (error) {
|
||||
set({error: error.response?.data?.message || error.message, isLoading: false});
|
||||
set({
|
||||
error: error.response?.data?.message || error.message,
|
||||
isLoading: false,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
signUp: async (userData) => {
|
||||
set({isLoading: true, error: null});
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
console.log(userData);
|
||||
|
||||
const response = await axios.post(`${API_URL}/auth/register`, userData, {
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
});
|
||||
|
||||
console.log(response.data);
|
||||
const response = await axios.post(
|
||||
`${API_URL}/auth/register`,
|
||||
userData,
|
||||
{
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}
|
||||
);
|
||||
|
||||
const user = response.data.user;
|
||||
const token = response.data.token;
|
||||
set({user, token, isLoading: false});
|
||||
|
||||
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});
|
||||
set({
|
||||
error: error.response?.data?.message || error.message,
|
||||
isLoading: false,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
signInWithGoogle: async (googleToken) => {
|
||||
set({isLoading: true, error: null});
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/auth/google`, {token: googleToken});
|
||||
|
||||
const {user, token} = response.data;
|
||||
set({user, token, isLoading: false});
|
||||
const response = await axios.post(`${API_URL}/auth/google`, {
|
||||
token: googleToken,
|
||||
});
|
||||
|
||||
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});
|
||||
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}` } : {};
|
||||
// Можно отправить запрос на бэкенд для инвалидации токена
|
||||
const { token } = get();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
await axios.post(`${API_URL}/auth/logout`, {}, { headers });
|
||||
} catch (error) {
|
||||
console.error("Logout error:", error);
|
||||
// console.error("Logout error:", error);
|
||||
} finally {
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
set({user: null, token: null});
|
||||
set({ user: null, token: null, isLoading: false });
|
||||
}
|
||||
},
|
||||
|
||||
checkAuth: async () => {
|
||||
const {token} = useAuthStore.getState();
|
||||
const { token } = get();
|
||||
if (!token) return null;
|
||||
|
||||
set({isLoading: true});
|
||||
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});
|
||||
set({ user: response.data, isLoading: false });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
set({user: null, token: null, isLoading: false});
|
||||
set({ user: null, token: null, isLoading: false });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
}),
|
||||
};
|
||||
},
|
||||
{
|
||||
name: "auth-storage",
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
|
||||
Reference in New Issue
Block a user