autoryzacja działa zarówno na android jak i ios

This commit is contained in:
2025-06-06 15:49:08 +02:00
parent b6f2225148
commit d4133f28bd
6 changed files with 53 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
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 {useRouter, Link} from 'expo-router';
@@ -18,6 +18,9 @@ import {Ionicons} from "@expo/vector-icons";
import * as WebBrowser from 'expo-web-browser';
import * as Google from "expo-auth-session/providers/google";
import AsyncStorage from "@react-native-async-storage/async-storage";
import {makeRedirectUri} from "expo-auth-session";
import Constants from 'expo-constants';
WebBrowser.maybeCompleteAuthSession();
@@ -27,13 +30,20 @@ WebBrowser.maybeCompleteAuthSession();
export default function Login() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const {signIn, isLoading} = useAuthStore();
const {signIn, isLoading, signInWithGoogle} = useAuthStore();
const router = useRouter();
const [request, response, promptAsync] = Google.useAuthRequest({
androidClientId: "936418008320-d8dfjph5e4r28fcm1rbdfbh5phmbg03d.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 () => {
@@ -56,13 +66,18 @@ export default function Login() {
}, [response]);
const handleGoogleLogin = async () => {
const user = await AsyncStorage.getItem("@user");
// const user = await AsyncStorage.getItem("@user");
let user = null;
if (!user) {
if(response?.type === "success") {
await getUserInfo(response.authentication.accessToken)
if(response.type === "success") {
user = await getUserInfo(response.authentication.accessToken)
await signInWithGoogle(response.authentication.accessToken);
alert(`Zalogowano jako ${user.email}`);
}
} 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();
console.log("Pobrano informacje o użytkowniku:", user);
await AsyncStorage.setItem("@user", JSON.stringify(user));
return user;
} catch (error) {
console.error("Błąd podczas pobierania informacji o użytkowniku:", error);
throw error;