-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignInScreen.tsx
185 lines (170 loc) · 4.97 KB
/
SignInScreen.tsx
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as React from 'react';
import {Button, Text, TextInput, View, FlatList, Pressable} from 'react-native';
import {Cred} from './security/SecureKeychain';
import {
AuthContext,
secureStorage,
styles,
UserNameServer,
createCredentialObj,
DeleteUsers,
} from './AppNav';
import { NostrServerList } from "./NostrServerList";
export function SignInScreen({navigation}) {
const {signIn} = React.useContext(AuthContext);
const [credentials, setCredentials] = React.useState<Array<Cred>>([]);
const [pubKey, setPubkey] = React.useState('');
const [privKey, setPrivKey] = React.useState('');
const [validPubKey, setValidPubKey] = React.useState(false);
const [validPrivKey, setValidPrivKey] = React.useState(false);
const [hidePrivKey, setHidePrivKey] = React.useState(true);
const [userName, setUserName] = React.useState('');
const [server, setServer] = React.useState('');
const [serverList, setServerList] = React.useState<Array<string>>();
const keySize: number = 64;
React.useEffect(() => {
setServerList(NostrServerList);
}, []);
const isValidPubKey = (key: string) => {
setPubkey(key);
key.length === keySize ? setValidPubKey(true) : setValidPubKey(false);
};
const isValidPrivKey = (key: string) => {
setPrivKey(key);
key.length === keySize ? setValidPrivKey(true) : setValidPrivKey(false);
};
React.useEffect(() => {
loadAllCreds();
}, []);
const loadAllCreds = async () => {
let creds: Cred[];
try {
// Restore token stored in `SecureStore` or any other encrypted storage
await secureStorage
.getAllCredentialsFromStore()
.then(tokens => {
console.log(
`[AppNav] :: [useEffect] :: token :: ${JSON.stringify(tokens)}`,
);
if (tokens) {
console.log(
`[AppNav] :: [useEffect] :: token :: ${JSON.stringify(tokens)}`,
);
creds = tokens;
} else {
console.log('[AppNav] :: [useEffect] :: No user found!');
//throw new Error("No user found!");
}
})
.catch(e => console.log('[AppNav] :: [useEffect] :: [catch] :: ' + e));
} catch (e) {
// Restoring token failed
//creds = secureStorage.generateCredentials("Demo User", "Demo Server");
console.log('[SignInScreen] :: [loadAllCreds] :: ' + e);
}
if (creds && creds.length > 0) {
setCredentials(creds);
return creds;
} else {
return false;
}
};
/*
const renderItem = ({data}) => {
return (
<Text>{data.username}</Text>
);
}
*/
const renderItem = ({item}: {item: Cred}) => {
/*
<Pressable onPress={() => signIn(item)}>
<Text>{item.username}</Text>
</Pressable>
*/
return (
<Pressable
onPress={() => signIn(item)}
style={({pressed}) => [
{
backgroundColor: pressed ? 'rgb(210, 230, 255)' : 'white',
},
styles.wrapperCustom,
]}>
<Text>
{item.username} :: {item.server}
</Text>
</Pressable>
);
};
//<Text style={{fontSize:32}}> {`\uF341`} Show Private Key </Text>
return (
<View>
<UserNameServer
userName={userName}
setUserName={setUserName}
server={server}
setServer={setServer}
serverList={serverList}
/>
<TextInput
placeholder="Public Key"
value={pubKey}
onChangeText={isValidPubKey}
/>
{!validPubKey && (
<Text style={{fontSize: 12}}>
*key should be {keySize} characters in length
</Text>
)}
<TextInput
placeholder="Private Key"
value={privKey}
onChangeText={isValidPrivKey}
secureTextEntry={hidePrivKey}
/>
<Pressable
onPress={() => {
setHidePrivKey(!hidePrivKey);
}}
style={({pressed}) => [
{
backgroundColor: pressed ? 'rgb(210, 230, 255)' : 'white',
},
styles.wrapperCustom,
]}>
<Text style={{fontSize: 20}}> {'\uF341'} Show Private Key </Text>
</Pressable>
{!validPrivKey && (
<Text style={{fontSize: 12}}>
*key should be {keySize} characters in length
</Text>
)}
<Button
title="Connect"
disabled={validPrivKey && validPubKey && server ? false : true}
onPress={() =>
signIn(createCredentialObj(privKey, pubKey, userName, server))
}
/>
<Button
title="Create account"
onPress={() => navigation.navigate('SignUp')}
/>
{credentials && credentials.length > 0 && (
<>
<Text>Or select from below users</Text>
<FlatList
//style={{margin: 40}}
data={credentials}
renderItem={renderItem}
keyExtractor={cred => cred.pubKey}
/>
<>
<DeleteUsers setCredentials={setCredentials} />
</>
</>
)}
</View>
);
}