Add signup (not work)

This commit is contained in:
Quentin Legot 2022-02-24 12:55:27 +01:00
parent 28d7a5a9b6
commit 6a82ff4c4d
9 changed files with 2026 additions and 552 deletions

2
.gitignore vendored
View File

@ -12,3 +12,5 @@ web-build/
# macOS # macOS
.DS_Store .DS_Store
.vscode

View File

@ -12,7 +12,7 @@ import SignUpScreen from '../Screen/SignUpScreen'
const Tab = createBottomTabNavigator() const Tab = createBottomTabNavigator()
export default function App() { export default function Navigation() {
return ( return (
<TokenContext.Consumer> <TokenContext.Consumer>
{([token, setToken]) => ( {([token, setToken]) => (

View File

@ -1,10 +1,9 @@
import React, { useContext, useState } from 'react' import React, { useContext, useState } from 'react'
import { Text, TextInput, StyleSheet } from 'react-native' import { Text, TextInput, StyleSheet, Button, View } from 'react-native'
import signIn from '../components/SignIn.js' import signIn from '../components/SignIn'
import { UsernameContext, TokenContext } from '../Context/Context.js' import { UsernameContext, TokenContext } from '../Context/Context'
export default function SignInScreen({ navigation }) {
export default function SignInScreen() {
const [ username, setUsername] = useContext(UsernameContext) const [ username, setUsername] = useContext(UsernameContext)
const [ token, setToken] = useContext(TokenContext) const [ token, setToken] = useContext(TokenContext)
const [ password, setPassword] = useState("") const [ password, setPassword] = useState("")
@ -14,8 +13,8 @@ export default function SignInScreen() {
signIn(username, password) signIn(username, password)
.then(token => { .then(token => {
setToken(token) setToken(token)
setUsername(login) setUsername(username)
props.navigate('Home') navigation.navigate('Home')
}) })
.catch(err => { .catch(err => {
setError(err) setError(err)
@ -23,21 +22,34 @@ export default function SignInScreen() {
} }
return ( return (
<> <View style={styles.container}>
<Text>{error}</Text> <Text>{error}</Text>
<Text>Nom d'utilisateur:</Text> <Text>Nom d'utilisateur:</Text>
<TextInput value={username} onChangeText={setUsername} onSubmitEditing={signin} style={styles.input}></TextInput> <TextInput value={username} onChangeText={setUsername} onSubmitEditing={signin} style={styles.input}></TextInput>
<Text>Mot de passe:</Text> <Text>Mot de passe:</Text>
<TextInput value={password} onChangeText={setPassword} onSubmitEditing={signin} style={styles.input} secureTextEntry={true}></TextInput> <TextInput value={password} onChangeText={setPassword} onSubmitEditing={signin} style={styles.input} secureTextEntry={true}></TextInput>
</> <View style={styles.button}>
<Button onPress={signin} title="Se connecter" />
</View>
</View>
); );
} }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
marginHorizontal: 16,
},
input: { input: {
height: 40, height: 40,
margin: 12, margin: 12,
borderWidth: 1, borderWidth: 1,
padding: 10, padding: 10,
}, },
button: {
padding: 10,
marginHorizontal: 30
}
}); });

View File

@ -1,10 +1,14 @@
import React from 'react' import React, { useContext } from 'react'
import {Text, View } from 'react-native' import {Text, View } from 'react-native'
import { UsernameContext, TokenContext } from '../Context/Context'
export default function App() { export default function App() {
const [ username, setUsername] = useContext(UsernameContext)
const [ token, setToken] = useContext(TokenContext)
setToken(null)
setUsername("")
return ( return (
<> <>
Page deconnexion
</> </>
); );
} }

View File

@ -1,10 +1,53 @@
import React from 'react' import React, { useContext, useState } from 'react'
import { Text, View } from 'react-native' import { Text, View, TextInput, Button, StyleSheet } from 'react-native'
import signUp from '../components/SignUp'
import { UsernameContext, TokenContext } from '../Context/Context'
export default function SignUpScreen({ navigation }) {
const [ username, setUsername] = useContext(UsernameContext)
const [ token, setToken] = useContext(TokenContext)
const [ password, setPassword] = useState("")
const [error, setError] = useState("")
const signup = () => {
signUp(username, password)
.then(token => {
setToken(token)
setUsername(username)
}).catch(err => setError(err))
}
export default function SignUpScreen() {
return ( return (
<> <View style={styles.container}>
Page Inscription <Text>{error}</Text>
</> <Text>Token is {token === undefined ? "undefined" : (token === null ? "null" : token)}</Text>
<Text>username is {username}</Text>
<Text>Nom d'utilisateur:</Text>
<TextInput value={username} onChangeText={setUsername} onSubmitEditing={signup} style={styles.input}></TextInput>
<Text>Mot de passe:</Text>
<TextInput value={password} onChangeText={setPassword} onSubmitEditing={signup} style={styles.input} secureTextEntry={true}></TextInput>
<View style={styles.button}>
<Button onPress={signup} title="S'inscrire" />
</View>
</View>
); );
} }
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
marginHorizontal: 16,
},
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
button: {
padding: 10,
marginHorizontal: 30
}
});

View File

@ -4,7 +4,7 @@ import { Text, View } from 'react-native'
export default function App() { export default function App() {
return ( return (
<> <>
Page todoLists <Text>Liste des TodoList</Text>
</> </>
); );
} }

32
components/SignUp.js Normal file
View File

@ -0,0 +1,32 @@
const API_URL = 'http://127.0.0.1:4000'
const SIGN_UP =
'mutation($username:String!, $password:String!){signUp(username:$username, password:$password)}'
export default function signUp (username, password) {
return fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: SIGN_UP,
variables: {
username: username,
password: password
}
})
})
.then(response => {
return response.json()
})
.then(jsonResponse => {
if (jsonResponse.errors != null){
throw(jsonResponse.errors[0].message)
}
return jsonResponse.data.signIn
})
.catch(error => {
throw error
})
}

2434
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,15 +12,16 @@
"dependencies": { "dependencies": {
"@react-navigation/bottom-tabs": "^6.2.0", "@react-navigation/bottom-tabs": "^6.2.0",
"@react-navigation/native": "^6.0.8", "@react-navigation/native": "^6.0.8",
"expo": "~44.0.0", "expo": "~44.0.6",
"expo-status-bar": "~1.2.0", "expo-status-bar": "~1.2.0",
"react": "17.0.1", "react": "17.0.2",
"react-dom": "17.0.1", "react-dom": "17.0.2",
"react-native": "0.64.3", "react-native": "0.67.2",
"react-native-web": "0.17.1" "react-native-web": "0.17.5",
"expo-dev-client": "~0.8.4"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.12.9" "@babel/core": "^7.17.2"
}, },
"private": true "private": true
} }