some fixes
This commit is contained in:
@@ -2,7 +2,7 @@ import axios from "axios";
|
|||||||
import FormData from "form-data";
|
import FormData from "form-data";
|
||||||
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";
|
||||||
|
|
||||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||||
|
|
||||||
@@ -36,7 +36,9 @@ export async function createNotice(notice) {
|
|||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${API_URL}/notices/add`, notice, {headers: headers});
|
const response = await axios.post(`${API_URL}/notices/add`, notice, {
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
|
||||||
if (response.data.noticeId !== null) {
|
if (response.data.noticeId !== null) {
|
||||||
for (const imageUri of notice.image) {
|
for (const imageUri of notice.image) {
|
||||||
@@ -71,7 +73,9 @@ export async function getAllImagesByNoticeId(noticeId) {
|
|||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
try {
|
try {
|
||||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`, {headers: headers});
|
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`, {
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
|
||||||
if (listResponse.data && listResponse.data.length > 0) {
|
if (listResponse.data && listResponse.data.length > 0) {
|
||||||
return listResponse.data.map((imageName) => ({
|
return listResponse.data.map((imageName) => ({
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import FormData from "form-data";
|
|
||||||
|
|
||||||
import { useAuthStore } from "@/store/authStore";
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
|
||||||
const API_URL = "https://hopp.zikor.pl/api/v1/orders";
|
const API_URL = "https://hopp.zikor.pl/api/v1/orders";
|
||||||
@@ -9,17 +7,16 @@ export async function createOrder(noticeId, orderType) {
|
|||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
const clientId = 1;
|
const clientId = 1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${API_URL}/add`,
|
`${API_URL}/add`,
|
||||||
{ clientId, noticeId, orderType },
|
{ clientId: clientId, noticeId: noticeId, orderType: orderType },
|
||||||
{
|
{
|
||||||
headers: {
|
headers: headers,
|
||||||
"Content-Type": "application/json",
|
|
||||||
...headers,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Error", error.response?.data, error.response?.status);
|
console.log("Error", error.response?.data, error.response?.status);
|
||||||
@@ -33,13 +30,10 @@ export async function createPayment(orderId) {
|
|||||||
const clientId = 1;
|
const clientId = 1;
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${API_URL}/token`,
|
`${API_URL}/token?orderId=${orderId}`,
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
headers: {
|
headers: headers,
|
||||||
"Content-Type": "application/json",
|
|
||||||
...headers,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return response.data;
|
return response.data;
|
||||||
@@ -49,13 +43,30 @@ export async function createPayment(orderId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getOrder(orderId) {
|
||||||
|
const { token } = useAuthStore.getState();
|
||||||
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.get(`${API_URL}/get/${orderId}`, { headers });
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
"Error fetching order:",
|
||||||
|
error.response?.data,
|
||||||
|
error.response?.status
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function listOrders() {
|
export async function listOrders() {
|
||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(`${API_URL}/get/all`, { headers });
|
const response = await axios.get(`${API_URL}/get/all`, { headers });
|
||||||
return response.data; // to będzie tablica OrderWithPaymentsDTO
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
"Error fetching orders:",
|
"Error fetching orders:",
|
||||||
|
|||||||
@@ -35,4 +35,4 @@ export async function getWishlist() {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
``
|
``;
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ import { Ionicons } from "@expo/vector-icons";
|
|||||||
import { useAuthStore } from "@/store/authStore";
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
|
||||||
export default function TabLayout() {
|
export default function TabLayout() {
|
||||||
const token = useAuthStore((state) => state.token);
|
const { token } = useAuthStore.getState();
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return <Redirect href="/login" />;
|
return <Redirect href="/login" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tabs
|
<Tabs
|
||||||
screenOptions={{
|
screenOptions={{
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { Text } from "@/components/ui/text";
|
|||||||
import { VStack } from "@/components/ui/vstack";
|
import { VStack } from "@/components/ui/vstack";
|
||||||
import { ActivityIndicator, FlatList } from "react-native";
|
import { ActivityIndicator, FlatList } from "react-native";
|
||||||
import { useEffect, useState, useRef } from "react";
|
import { useEffect, useState, useRef } from "react";
|
||||||
import { createOrder, createPayment } from "@/api/order";
|
import { createOrder, createPayment, getOrder } from "@/api/order";
|
||||||
import { Linking } from "react-native";
|
import { Linking } from "react-native";
|
||||||
import { Ionicons } from "@expo/vector-icons";
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
import { useToast, Toast, ToastTitle } from "@/components/ui/toast";
|
import { useToast, Toast, ToastTitle } from "@/components/ui/toast";
|
||||||
@@ -22,27 +22,34 @@ export default function UserNotices() {
|
|||||||
const appState = useRef(AppState.currentState);
|
const appState = useRef(AppState.currentState);
|
||||||
const [toastId, setToastId] = useState(0);
|
const [toastId, setToastId] = useState(0);
|
||||||
const { user_id } = useAuthStore.getState();
|
const { user_id } = useAuthStore.getState();
|
||||||
const currentUserId= user_id;
|
const currentUserId = user_id;
|
||||||
|
const [orderId, setOrderId] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isRedirecting) return;
|
if (!isRedirecting) return;
|
||||||
const subscription = AppState.addEventListener("change", (state) => {
|
const subscription = AppState.addEventListener("change", (state) => {
|
||||||
if (state === "active") {
|
if (state === "active") {
|
||||||
setIsRedirecting(false);
|
(async () => {
|
||||||
const paymentStatus = "CORRECT";
|
const lastOrder = await getOrder(orderId);
|
||||||
if (paymentStatus === "INCORRECT") {
|
const lastPayments = lastOrder.payments;
|
||||||
showNewToast("Płatność została anulowana.");
|
const paymentStatus =
|
||||||
} else if (paymentStatus === "CORRECT") {
|
lastPayments.length > 0
|
||||||
showNewToast("Płatność została zrealizowana.");
|
? lastPayments[lastPayments.length - 1].status
|
||||||
} else {
|
: null;
|
||||||
showNewToast("Płatność jeszcze nie wpłynęła.");
|
setIsRedirecting(false);
|
||||||
}
|
if (paymentStatus === "INCORRECT") {
|
||||||
|
showNewToast("Płatność została anulowana.");
|
||||||
|
} else if (paymentStatus === "CORRECT") {
|
||||||
|
showNewToast("Płatność została zrealizowana.");
|
||||||
|
} else {
|
||||||
|
showNewToast("Płatność jeszcze nie wpłynęła.");
|
||||||
|
}
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
appState.current = state;
|
appState.current = state;
|
||||||
});
|
});
|
||||||
return () => subscription.remove();
|
return () => subscription.remove();
|
||||||
}, [isRedirecting, toast]);
|
}, [isRedirecting, toast, orderId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadNotices = async () => {
|
const loadNotices = async () => {
|
||||||
@@ -81,23 +88,23 @@ export default function UserNotices() {
|
|||||||
try {
|
try {
|
||||||
const result = await createOrder(noticeId, type);
|
const result = await createOrder(noticeId, type);
|
||||||
if (result) {
|
if (result) {
|
||||||
|
setOrderId(result);
|
||||||
try {
|
try {
|
||||||
const paymentResult = await createPayment(); //trzeba dodać orderId
|
const paymentResult = await createPayment(result);
|
||||||
if (paymentResult) {
|
if (paymentResult) {
|
||||||
setIsRedirecting(true);
|
setIsRedirecting(true);
|
||||||
await Linking.openURL(paymentResult);
|
await Linking.openURL(paymentResult);
|
||||||
setWaitingForPayment(true);
|
|
||||||
} else {
|
} else {
|
||||||
console.log(`Nie udało się aktywować ogłoszenia ${noticeId}.`);
|
console.log(`Nie udało się aktywować ogłoszenia 4 ${noticeId}.`);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
// console.log("Błąd podczas aktywacji ogłoszenia 3:", err);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`Nie udało się aktywować ogłoszenia ${noticeId}.`);
|
// console.log(`Nie udało się aktywować ogłoszenia 2 ${noticeId}.`);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
console.log("Błąd podczas aktywacji ogłoszenia 1:", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -116,9 +123,9 @@ export default function UserNotices() {
|
|||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<Box className="items-center justify-center flex-1">
|
<Box className="items-center justify-center flex-1">
|
||||||
<ActivityIndicator size="large" color="#787878"/>
|
<ActivityIndicator size="large" color="#787878" />
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,38 +148,42 @@ export default function UserNotices() {
|
|||||||
<NoticeCard notice={item} />
|
<NoticeCard notice={item} />
|
||||||
<Box className="flex-row justify-between mt-2">
|
<Box className="flex-row justify-between mt-2">
|
||||||
<Button
|
<Button
|
||||||
className="ml-2"
|
className="ml-2"
|
||||||
onPress={() => handleDeleteNotice(item.noticeId)}
|
onPress={() => handleDeleteNotice(item.noticeId)}
|
||||||
size="md"
|
size="md"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
action="primary"
|
action="primary"
|
||||||
>
|
>
|
||||||
<ButtonText>Usuń</ButtonText>
|
<ButtonText>Usuń</ButtonText>
|
||||||
<Ionicons name="trash-outline" size={14} />
|
<Ionicons name="trash-outline" size={14} />
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
{item.status === "ACTIVE" ? (
|
{item.status === "ACTIVE" ? (
|
||||||
<Button
|
<Button
|
||||||
className="mr-2"
|
className="mr-2"
|
||||||
size="md"
|
size="md"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
action="primary"
|
action="primary"
|
||||||
onPress={() => handleOrder(item.noticeId, "BOOST")}
|
onPress={() => handleOrder(item.noticeId, "BOOST")}
|
||||||
>
|
>
|
||||||
<ButtonText>Podbij</ButtonText>
|
<ButtonText>Podbij</ButtonText>
|
||||||
<Ionicons name="arrow-up" size={14} color="#fff" />
|
<Ionicons name="arrow-up" size={14} color="#fff" />
|
||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<Button
|
<Button
|
||||||
className="mr-2"
|
className="mr-2"
|
||||||
size="md"
|
size="md"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
action="primary"
|
action="primary"
|
||||||
onPress={() => handleOrder(item.noticeId, "ACTIVATE")}
|
onPress={() => handleOrder(item.noticeId, "ACTIVATION")}
|
||||||
>
|
>
|
||||||
<ButtonText>Aktywuj</ButtonText>
|
<ButtonText>Aktywuj</ButtonText>
|
||||||
<Ionicons name="arrow-redo-outline" size={14} color="#fff" />
|
<Ionicons
|
||||||
</Button>
|
name="arrow-redo-outline"
|
||||||
|
size={14}
|
||||||
|
color="#fff"
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@@ -32,11 +32,8 @@ export default function Home() {
|
|||||||
}, [token, fetchNotices]);
|
}, [token, fetchNotices]);
|
||||||
|
|
||||||
const notices = useNoticesStore((state) => state.notices);
|
const notices = useNoticesStore((state) => state.notices);
|
||||||
// console.log("Notices:", notices);
|
|
||||||
// console.log("Notices length:", notices.length);
|
|
||||||
|
|
||||||
const activeNotices = notices.filter((notice) => notice.status === "ACTIVE");
|
const activeNotices = notices.filter((notice) => notice.status === "ACTIVE");
|
||||||
// console.log("Activer Notices:", activeNotices.length);
|
|
||||||
const latestNotices = [...activeNotices]
|
const latestNotices = [...activeNotices]
|
||||||
.sort((a, b) => new Date(b.publishDate) - new Date(a.publishDate))
|
.sort((a, b) => new Date(b.publishDate) - new Date(a.publishDate))
|
||||||
.slice(0, 6);
|
.slice(0, 6);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {create} from "zustand";
|
import { create } from "zustand";
|
||||||
import {createJSONStorage, persist} from "zustand/middleware";
|
import { createJSONStorage, persist } from "zustand/middleware";
|
||||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
@@ -8,135 +8,135 @@ const API_URL = "https://hopp.zikor.pl/api/v1";
|
|||||||
let interceptorInitialized = false;
|
let interceptorInitialized = false;
|
||||||
|
|
||||||
export const useAuthStore = create(
|
export const useAuthStore = create(
|
||||||
persist(
|
persist(
|
||||||
(set, get) => {
|
(set, get) => {
|
||||||
if (!interceptorInitialized.current) {
|
// if (!interceptorInitialized.current) {
|
||||||
axios.interceptors.response.use(
|
// axios.interceptors.response.use(
|
||||||
(response) => response,
|
// (response) => response,
|
||||||
(error) => {
|
// (error) => {
|
||||||
if (
|
// if (
|
||||||
(error.response && error.response.status === 401) ||
|
// (error.response && error.response.status === 401) ||
|
||||||
error.response.status === 403
|
// error.response.status === 403
|
||||||
) {
|
// ) {
|
||||||
set({user_id: null, token: null, isLoading: false});
|
// set({ user_id: null, token: null, isLoading: false });
|
||||||
delete axios.defaults.headers.common["Authorization"];
|
// delete axios.defaults.headers.common["Authorization"];
|
||||||
}
|
// }
|
||||||
return Promise.reject(error);
|
// return Promise.reject(error);
|
||||||
}
|
// }
|
||||||
);
|
// );
|
||||||
interceptorInitialized = true;
|
// interceptorInitialized = true;
|
||||||
}
|
// }
|
||||||
return {
|
return {
|
||||||
user_id: null,
|
user_id: null,
|
||||||
token: null,
|
token: null,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
error: null,
|
error: null,
|
||||||
|
|
||||||
signIn: async (email, password) => {
|
signIn: async (email, password) => {
|
||||||
set({isLoading: true, error: null});
|
set({ isLoading: true, error: null });
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${API_URL}/auth/login`, {
|
const response = await axios.post(`${API_URL}/auth/login`, {
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
|
|
||||||
const user_id = response.data.user_id;
|
const user_id = response.data.user_id;
|
||||||
const token = response.data.token;
|
const token = response.data.token;
|
||||||
set({user_id: user_id, token: token, isLoading: false});
|
set({ user_id: user_id, token: token, isLoading: false });
|
||||||
|
|
||||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
set({
|
set({
|
||||||
error: error.response?.data?.message || error.message,
|
error: error.response?.data?.message || error.message,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
});
|
});
|
||||||
throw error;
|
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;
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "auth-storage",
|
signUp: async (userData) => {
|
||||||
storage: createJSONStorage(() => AsyncStorage),
|
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),
|
||||||
|
}
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user