From 32460062b8fc9e86279449a610d2ecda846c9bd7 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 9 Dec 2024 22:29:48 +0100 Subject: [PATCH] Ajout connexion au serveur --- client/app/(tabs)/settings.tsx | 18 +++++--- client/app/_layout.tsx | 1 + client/app/login.tsx | 77 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 client/app/login.tsx diff --git a/client/app/(tabs)/settings.tsx b/client/app/(tabs)/settings.tsx index 2910488..413ddc2 100644 --- a/client/app/(tabs)/settings.tsx +++ b/client/app/(tabs)/settings.tsx @@ -1,12 +1,18 @@ -import { FlatList } from 'react-native' -import { Surface, Text } from 'react-native-paper' -// import * as SecureStore from 'expo-secure-store' +import { useRouter } from 'expo-router' +import { FAB, List, Surface } from 'react-native-paper' +import * as SecureStore from 'expo-secure-store' export default function HistoryScreen() { - // const token = SecureStore.getItem("apiToken") + const router = useRouter() + const isLoggedIn = SecureStore.getItem("apiToken") !== null return ( - - {item}} /> + + router.navigate('/login')} />} + onPress={() => router.navigate('/login')} /> ) } diff --git a/client/app/_layout.tsx b/client/app/_layout.tsx index 63b6859..00d24d9 100644 --- a/client/app/_layout.tsx +++ b/client/app/_layout.tsx @@ -17,6 +17,7 @@ export default function RootLayout() { + diff --git a/client/app/login.tsx b/client/app/login.tsx new file mode 100644 index 0000000..7300f21 --- /dev/null +++ b/client/app/login.tsx @@ -0,0 +1,77 @@ +import { useRouter } from "expo-router" +import { useRef, useState } from "react" +import { Button, Dialog, Portal, Surface, Text, TextInput } from "react-native-paper" +import * as SecureStore from 'expo-secure-store' + +export default function Login() { + const router = useRouter() + const [name, setName] = useState(SecureStore.getItem('apiName') ?? "") + const [password, setPassword] = useState("") + const [errorDialogVisible, setErrorDialogVisible] = useState(false) + const [errorTitle, setErrorTitle] = useState("") + const [errorText, setErrorText] = useState("") + + const loginRef = useRef() + const passwordRef = useRef() + + const hideErrorDialog = () => setErrorDialogVisible(false) + + async function onLogin() { + const resp = await fetch("http://192.168.1.198:3000/auth/login/", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ name: name, password: password }) + }) + .then(resp => resp.json()) + .catch(err => { + setErrorDialogVisible(true) + setErrorTitle("Erreur") + setErrorText("Une erreur inconnue est survenue lors de la connexion. Veuillez réessayer plus tard. " + err) + }) + console.log(resp) + if (resp.error) { + setErrorDialogVisible(true) + setErrorTitle(resp.error) + setErrorText(resp.message) + return + } + SecureStore.setItem("apiName", name) + SecureStore.setItem("apiPassword", password) + SecureStore.setItem("apiToken", resp.accessToken) + router.back() + } + + return ( + + setName(text)} + onSubmitEditing={() => passwordRef?.current.focus()} + style={{ margin: 8 }} /> + setPassword(text)} + onSubmitEditing={onLogin} + secureTextEntry={true} + style={{ margin: 8 }} /> + + + + {errorTitle} + + {errorText} + + + + + + + + ) +}