autoryzacja działa zarówno na android jak i ios
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import axios from "axios";
|
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 listCategories() {
|
export async function listCategories() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -4,18 +4,15 @@ import {useAuthStore} from "@/store/authStore";
|
|||||||
|
|
||||||
// const API_URL = "https://testowe.zikor.pl/api/v1";
|
// const API_URL = "https://testowe.zikor.pl/api/v1";
|
||||||
|
|
||||||
const API_URL = "http://10.0.2.2:8080/api/v1";
|
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||||
|
|
||||||
export async function listNotices() {
|
export async function listNotices() {
|
||||||
const { token } = useAuthStore.getState();
|
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`, {
|
const response = await fetch(`${API_URL}/notices/get/all`, {
|
||||||
headers: headers
|
headers: headers
|
||||||
});
|
});
|
||||||
console.log(response);
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(response.toString());
|
throw new Error(response.toString());
|
||||||
@@ -62,8 +59,6 @@ export async function getImageByNoticeId(noticeId) {
|
|||||||
const imageName = listResponse.data[0];
|
const imageName = listResponse.data[0];
|
||||||
imageUrl = `${API_URL}/images/get/${imageName}`;
|
imageUrl = `${API_URL}/images/get/${imageName}`;
|
||||||
|
|
||||||
console.log(`Pobrano zdjęcie o nazwie: ${imageName}`);
|
|
||||||
|
|
||||||
return imageUrl;
|
return imageUrl;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(`Zdjęcie nie istnieje dla notice o id: ${noticeId}`);
|
console.log(`Zdjęcie nie istnieje dla notice o id: ${noticeId}`);
|
||||||
@@ -77,25 +72,23 @@ export async function getAllImagesByNoticeId(noticeId) {
|
|||||||
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
const listResponse = await axios.get(`${API_URL}/images/list/${noticeId}`);
|
||||||
|
|
||||||
if (listResponse.data && listResponse.data.length > 0) {
|
if (listResponse.data && listResponse.data.length > 0) {
|
||||||
const imageUrls = listResponse.data.map(imageName =>
|
return listResponse.data.map(imageName =>
|
||||||
`${API_URL}/images/get/${imageName}`
|
`${API_URL}/images/get/${imageName}`
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(`Pobrano ${imageUrls.length} zdjęć dla ogłoszenia o id: ${noticeId}`);
|
|
||||||
return imageUrls;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Brak zdjęć dla ogłoszenia o id: ${noticeId}`);
|
|
||||||
return ["https://http.cat/404.jpg"];
|
return ["https://http.cat/404.jpg"];
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(`Błąd podczas pobierania listy zdjęć dla ogłoszenia o id: ${noticeId}`, err);
|
if(err.response.status === 404) {
|
||||||
|
console.info(`Ogłoszenie o id: ${noticeId} nie posiada zdjęć.`);
|
||||||
|
return ["https://http.cat/404.jpg"];
|
||||||
|
}
|
||||||
|
console.warn(`Nie udało się pobrać listy zdjęć dla ogłoszenia o id: ${noticeId}`, err);
|
||||||
return ["https://http.cat/404.jpg"];
|
return ["https://http.cat/404.jpg"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uploadImage = async (noticeId, imageUri) => {
|
export const uploadImage = async (noticeId, imageUri) => {
|
||||||
console.log(imageUri);
|
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
const filename = imageUri.split('/').pop();
|
const filename = imageUri.split('/').pop();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"expo": {
|
"expo": {
|
||||||
"name": "ArtisanConnect",
|
"name": "ArtisanConnect",
|
||||||
"slug": "ArtisanConnect",
|
"slug": "ArtisanConnect",
|
||||||
"scheme": "ArtisanConnect",
|
"scheme": "com.hamx.artisanconnect",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"orientation": "portrait",
|
"orientation": "portrait",
|
||||||
"icon": "./assets/icon.png",
|
"icon": "./assets/icon.png",
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
},
|
},
|
||||||
"ios": {
|
"ios": {
|
||||||
"supportsTablet": true,
|
"supportsTablet": true,
|
||||||
"bundleIdentifier": "com.hamx.ArtisanConnect"
|
"bundleIdentifier": "com.hamx.artisanconnect"
|
||||||
},
|
},
|
||||||
"android": {
|
"android": {
|
||||||
"adaptiveIcon": {
|
"adaptiveIcon": {
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
"android.permission.RECORD_AUDIO",
|
"android.permission.RECORD_AUDIO",
|
||||||
"android.permission.CAMERA"
|
"android.permission.CAMERA"
|
||||||
],
|
],
|
||||||
"package": "com.hamx.ArtisanConnect"
|
"package": "com.hamx.artisanconnect"
|
||||||
},
|
},
|
||||||
"web": {
|
"web": {
|
||||||
"favicon": "./assets/favicon.png"
|
"favicon": "./assets/favicon.png"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, {useEffect, useState} from 'react';
|
import React, {useEffect, useState} from 'react';
|
||||||
import {StyleSheet, ActivityIndicator, SafeAreaView, View} from 'react-native';
|
import {StyleSheet, ActivityIndicator, SafeAreaView, View, Platform} from 'react-native';
|
||||||
import {useAuthStore} from '@/store/authStore';
|
import {useAuthStore} from '@/store/authStore';
|
||||||
import {useRouter, Link} from 'expo-router';
|
import {useRouter, Link} from 'expo-router';
|
||||||
|
|
||||||
@@ -18,6 +18,9 @@ import {Ionicons} from "@expo/vector-icons";
|
|||||||
import * as WebBrowser from 'expo-web-browser';
|
import * as WebBrowser from 'expo-web-browser';
|
||||||
import * as Google from "expo-auth-session/providers/google";
|
import * as Google from "expo-auth-session/providers/google";
|
||||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
|
import {makeRedirectUri} from "expo-auth-session";
|
||||||
|
|
||||||
|
import Constants from 'expo-constants';
|
||||||
|
|
||||||
WebBrowser.maybeCompleteAuthSession();
|
WebBrowser.maybeCompleteAuthSession();
|
||||||
|
|
||||||
@@ -27,13 +30,20 @@ WebBrowser.maybeCompleteAuthSession();
|
|||||||
export default function Login() {
|
export default function Login() {
|
||||||
const [email, setEmail] = useState('');
|
const [email, setEmail] = useState('');
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
const {signIn, isLoading} = useAuthStore();
|
const {signIn, isLoading, signInWithGoogle} = useAuthStore();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const [request, response, promptAsync] = Google.useAuthRequest({
|
const [request, response, promptAsync] = Google.useAuthRequest({
|
||||||
androidClientId: "936418008320-d8dfjph5e4r28fcm1rbdfbh5phmbg03d.apps.googleusercontent.com",
|
androidClientId: "936418008320-d8dfjph5e4r28fcm1rbdfbh5phmbg03d.apps.googleusercontent.com",
|
||||||
iosClientId: "936418008320-ohefdfcebd41f6oa2o8phh1mgj9s49sl.apps.googleusercontent.com",
|
iosClientId: "936418008320-ohefdfcebd41f6oa2o8phh1mgj9s49sl.apps.googleusercontent.com",
|
||||||
webClientId: "936418008320-btdngtlfnjac1p67guje72m9el5q59a7.apps.googleusercontent.com"
|
webClientId: "936418008320-btdngtlfnjac1p67guje72m9el5q59a7.apps.googleusercontent.com",
|
||||||
|
redirectUri:
|
||||||
|
Platform.OS === 'android'
|
||||||
|
? makeRedirectUri({
|
||||||
|
scheme: Constants.expoConfig.android.package,
|
||||||
|
path: '/',
|
||||||
|
})
|
||||||
|
: undefined,
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleInternalLogin = async () => {
|
const handleInternalLogin = async () => {
|
||||||
@@ -56,13 +66,18 @@ export default function Login() {
|
|||||||
}, [response]);
|
}, [response]);
|
||||||
|
|
||||||
const handleGoogleLogin = async () => {
|
const handleGoogleLogin = async () => {
|
||||||
const user = await AsyncStorage.getItem("@user");
|
// const user = await AsyncStorage.getItem("@user");
|
||||||
|
let user = null;
|
||||||
if (!user) {
|
if (!user) {
|
||||||
if(response?.type === "success") {
|
if(response.type === "success") {
|
||||||
await getUserInfo(response.authentication.accessToken)
|
user = await getUserInfo(response.authentication.accessToken)
|
||||||
|
await signInWithGoogle(response.authentication.accessToken);
|
||||||
|
alert(`Zalogowano jako ${user.email}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Set the user in the auth store
|
console.info("Pobrano użytkownika z AsyncStorage:", JSON.parse(user));
|
||||||
|
alert(`Zalogowano jako ${user.email}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -79,8 +94,8 @@ export default function Login() {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
const user = await response.json();
|
const user = await response.json();
|
||||||
console.log("Pobrano informacje o użytkowniku:", user);
|
|
||||||
await AsyncStorage.setItem("@user", JSON.stringify(user));
|
await AsyncStorage.setItem("@user", JSON.stringify(user));
|
||||||
|
return user;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Błąd podczas pobierania informacji o użytkowniku:", error);
|
console.error("Błąd podczas pobierania informacji o użytkowniku:", error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
"expo": "^53.0.0",
|
"expo": "^53.0.0",
|
||||||
"expo-auth-session": "~6.1.5",
|
"expo-auth-session": "~6.1.5",
|
||||||
"expo-camera": "~16.1.6",
|
"expo-camera": "~16.1.6",
|
||||||
"expo-constants": "~17.1.5",
|
"expo-constants": "~17.1.6",
|
||||||
"expo-image-picker": "~16.1.4",
|
"expo-image-picker": "~16.1.4",
|
||||||
"expo-linking": "~7.1.4",
|
"expo-linking": "~7.1.4",
|
||||||
"expo-router": "~5.0.5",
|
"expo-router": "~5.0.5",
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ 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";
|
||||||
|
|
||||||
const API_URL = "http://10.0.2.2:8080/api/v1";
|
const API_URL = "https://hopp.zikor.pl/api/v1";
|
||||||
|
|
||||||
export const useAuthStore = create(
|
export const useAuthStore = create(
|
||||||
persist(
|
persist(
|
||||||
(set) => ({
|
(set) => ({
|
||||||
user: null,
|
user_id: null,
|
||||||
token: null,
|
token: null,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
error: null,
|
error: null,
|
||||||
@@ -21,9 +21,11 @@ export const useAuthStore = create(
|
|||||||
password
|
password
|
||||||
});
|
});
|
||||||
|
|
||||||
const user = response.data.user;
|
const user_id = response.data.user_id;
|
||||||
const token = response.data.token;
|
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) {
|
} catch (error) {
|
||||||
set({error: error.response?.data?.message || error.message, isLoading: false});
|
set({error: error.response?.data?.message || error.message, isLoading: false});
|
||||||
throw error;
|
throw error;
|
||||||
@@ -33,19 +35,15 @@ export const useAuthStore = create(
|
|||||||
signUp: async (userData) => {
|
signUp: async (userData) => {
|
||||||
set({isLoading: true, error: null});
|
set({isLoading: true, error: null});
|
||||||
try {
|
try {
|
||||||
console.log(userData);
|
|
||||||
|
|
||||||
const response = await axios.post(`${API_URL}/auth/register`, userData, {
|
const response = await axios.post(`${API_URL}/auth/register`, userData, {
|
||||||
headers: {'Content-Type': 'application/json'}
|
headers: {'Content-Type': 'application/json'}
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(response.data);
|
const user_id = response.data.user_id;
|
||||||
|
|
||||||
const user = response.data.user;
|
|
||||||
const token = response.data.token;
|
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) {
|
} catch (error) {
|
||||||
set({error: error.response?.data?.message || error.message, isLoading: false});
|
set({error: error.response?.data?.message || error.message, isLoading: false});
|
||||||
throw error;
|
throw error;
|
||||||
@@ -55,14 +53,14 @@ export const useAuthStore = create(
|
|||||||
signInWithGoogle: async (googleToken) => {
|
signInWithGoogle: async (googleToken) => {
|
||||||
set({isLoading: true, error: null});
|
set({isLoading: true, error: null});
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${API_URL}/auth/google`, {token: googleToken});
|
const response = await axios.post(`${API_URL}/auth/google`, {googleToken: googleToken}, {
|
||||||
|
headers: {'Content-Type': 'application/json'}
|
||||||
const {user, token} = response.data;
|
});
|
||||||
set({user, token, isLoading: false});
|
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}`;
|
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
||||||
|
|
||||||
return user;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
set({error: error.response?.data?.message || error.message, isLoading: false});
|
set({error: error.response?.data?.message || error.message, isLoading: false});
|
||||||
throw error;
|
throw error;
|
||||||
@@ -71,13 +69,12 @@ export const useAuthStore = create(
|
|||||||
|
|
||||||
signOut: async () => {
|
signOut: async () => {
|
||||||
try {
|
try {
|
||||||
// Можно отправить запрос на бэкенд для инвалидации токена
|
|
||||||
await axios.post(`${API_URL}/auth/logout`);
|
await axios.post(`${API_URL}/auth/logout`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Logout error:", error);
|
console.error("Logout error:", error);
|
||||||
} finally {
|
} finally {
|
||||||
delete axios.defaults.headers.common["Authorization"];
|
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`);
|
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;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
delete axios.defaults.headers.common["Authorization"];
|
delete axios.defaults.headers.common["Authorization"];
|
||||||
set({user: null, token: null, isLoading: false});
|
set({user_id: null, token: null, isLoading: false});
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user