autoryzacja działa zarówno na android jak i ios
This commit is contained in:
@@ -3,12 +3,12 @@ import {createJSONStorage, persist} from "zustand/middleware";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import axios from "axios";
|
||||
|
||||
const API_URL = "http://10.0.2.2:8080/api/v1";
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
export const useAuthStore = create(
|
||||
persist(
|
||||
(set) => ({
|
||||
user: null,
|
||||
user_id: null,
|
||||
token: null,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
@@ -21,9 +21,11 @@ export const useAuthStore = create(
|
||||
password
|
||||
});
|
||||
|
||||
const user = response.data.user;
|
||||
const user_id = response.data.user_id;
|
||||
const token = response.data.token;
|
||||
set({user, token, isLoading: false});
|
||||
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;
|
||||
@@ -33,19 +35,15 @@ export const useAuthStore = create(
|
||||
signUp: async (userData) => {
|
||||
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 user = response.data.user;
|
||||
const user_id = response.data.user_id;
|
||||
const token = response.data.token;
|
||||
set({user, token, isLoading: false});
|
||||
set({user_id: user_id, token: token, isLoading: false});
|
||||
|
||||
return user;
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
} catch (error) {
|
||||
set({error: error.response?.data?.message || error.message, isLoading: false});
|
||||
throw error;
|
||||
@@ -55,14 +53,14 @@ export const useAuthStore = create(
|
||||
signInWithGoogle: async (googleToken) => {
|
||||
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`, {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}`;
|
||||
|
||||
return user;
|
||||
} catch (error) {
|
||||
set({error: error.response?.data?.message || error.message, isLoading: false});
|
||||
throw error;
|
||||
@@ -71,13 +69,12 @@ export const useAuthStore = create(
|
||||
|
||||
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: null, token: null});
|
||||
set({user_id: null, token: null});
|
||||
}
|
||||
},
|
||||
|
||||
@@ -91,11 +88,11 @@ export const useAuthStore = create(
|
||||
|
||||
const response = await axios.get(`${API_URL}/auth/me`);
|
||||
|
||||
set({user: response.data, isLoading: false});
|
||||
set({user_id: response.data, isLoading: false});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
set({user: null, token: null, isLoading: false});
|
||||
set({user_id: null, token: null, isLoading: false});
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user