forked from STEDI-Balance/stedi-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Login.js
97 lines (83 loc) · 2.09 KB
/
Login.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// @ts = check
import React from "react";
import { SafeAreaView, StyleSheet, TextInput, Button } from "react-native";
export default function Login(props){
const [text, onChangeText] = React.useState("");
const [number, onChangeNumber] = React.useState(null);
return (
<SafeAreaView>
<TextInput
style={styles.input}
onChangeText={onChangeText}
value={text}
placeholder="Phone Number"
keyboardType="numeric"
/>
<Button title="Send One Time Password" onPress={()=>sendCode(text)}></Button>
<TextInput
style={styles.input}
onChangeText={onChangeNumber}
value={number}
placeholder="One Time Password"
keyboardType="numeric"
/>
<Button title="log In" onPress={()=>
checkCode(text, number, props)}>
</Button>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
});
async function sendCode (phoneNumber) {
await fetch(('https://dev.stedi.me/twofactorlogin/' + phoneNumber), {
method: 'POST',
headers:{
Accept:"application/json",
"Content-Type": "application/json"
},
});
}
async function checkCode (phoneNumber, oneTimePassword, props) {
let token = "";
try {
let response = await fetch('https://dev.stedi.me/twofactorlogin', {
method: 'POST',
headers:{
Accept:"application/text",
"Content-Type":"application/text",
},
body: JSON.stringify ({
phoneNumber: phoneNumber,
oneTimePassword: oneTimePassword,
}),
})
token = await response.text()
if (response.status == 200) {
props.setUserLoggedIn(true);
}
console.log (response.status);
}
catch(error){
console.log(error)
}
try {
let response = await fetch('https://dev.stedi.me/validate/' + token, {
method: 'GET',
headers: {
Accept:"application/text",
"Content-Type":"application/text"
}
})
const email = await response.text();
props.setUserEmail(email);
}
catch(error){
console.log(error)
}};