Przeniesiono autoryzacje do osobnego pliku api/auth.jsx
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import axios from "axios";
|
||||
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
export async function login(userData) {
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/auth/login`, userData, {
|
||||
headers: {"Content-Type": "application/json"},
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Login failed:", error);
|
||||
throw error.response?.data?.message || "Login failed";
|
||||
}
|
||||
}
|
||||
|
||||
export async function register(userData) {
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/auth/register`, userData, {
|
||||
headers: {"Content-Type": "application/json"},
|
||||
});
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Registration failed:", error);
|
||||
throw error.response?.data?.message || "Registration failed";
|
||||
}
|
||||
}
|
||||
|
||||
export async function googleLogin(googleToken) {
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/auth/google`,
|
||||
{ googleToken: googleToken },
|
||||
{
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Google login failed:", error);
|
||||
throw error.response?.data?.message || "Google login failed";
|
||||
}
|
||||
}
|
||||
|
||||
export async function logout(token) {
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/auth/logout`,
|
||||
{},
|
||||
{
|
||||
headers: headers,
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error("Logout failed:", error);
|
||||
throw error.response?.data?.message || "Logout failed";
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,9 @@ import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import axios from "axios";
|
||||
import * as api from "@/api/auth";
|
||||
import { router } from "expo-router";
|
||||
|
||||
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||
|
||||
let interceptorInitialized = false;
|
||||
|
||||
export const useAuthStore = create(
|
||||
@@ -38,16 +37,8 @@ export const useAuthStore = create(
|
||||
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 });
|
||||
|
||||
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||
const response = await api.login({email, password});
|
||||
set({ user_id: response.user_id, token: response.token, isLoading: false });
|
||||
} catch (error) {
|
||||
set({
|
||||
error: error.response?.data?.message || error.message,
|
||||
@@ -60,19 +51,8 @@ export const useAuthStore = create(
|
||||
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}`;
|
||||
const response = await api.register(userData);
|
||||
set({ user_id: response.user_id, token: response.token, isLoading: false });
|
||||
} catch (error) {
|
||||
set({
|
||||
error: error.response?.data?.message || error.message,
|
||||
@@ -85,18 +65,8 @@ export const useAuthStore = create(
|
||||
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}`;
|
||||
const response = await api.googleLogin(googleToken);
|
||||
set({ user_id: response.user_id, token: response.token, isLoading: false });
|
||||
} catch (error) {
|
||||
set({
|
||||
error: error.response?.data?.message || error.message,
|
||||
@@ -108,19 +78,11 @@ export const useAuthStore = create(
|
||||
|
||||
signOut: async () => {
|
||||
const { token } = get();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
try {
|
||||
await axios.post(
|
||||
`${API_URL}/auth/logout`,
|
||||
{},
|
||||
{
|
||||
headers: headers,
|
||||
}
|
||||
);
|
||||
await api.logout(token);
|
||||
} catch (error) {
|
||||
console.error("Logout error:", error);
|
||||
} finally {
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
set({ user_id: null, token: null });
|
||||
router.replace("/login");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user