forked from Sefaria/Sefaria-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthPage.js
296 lines (279 loc) · 9.95 KB
/
AuthPage.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict';
import React, { useState, useContext } from 'react';
import PropTypes from 'prop-types';
import {
View,
ScrollView,
Text,
TextInput,
TouchableOpacity,
KeyboardAvoidingView,
Platform,
Image,
} from 'react-native';
import remoteConfig from '@react-native-firebase/remote-config';
import {
RainbowBar,
CircleCloseButton,
SystemButton,
} from './Misc';
import { GlobalStateContext, DispatchContext, STATE_ACTIONS, getTheme } from './StateManager';
import Sefaria from './sefaria';
import strings from './LocalizedStrings';
import styles from './Styles';
const onSubmit = async (formState, authMode, setErrors, onLoginSuccess, setIsLoading) => {
setIsLoading(true);
const mobileAppKey = await getMobileAppKey();
formState.mobile_app_key = mobileAppKey;
let errors = await Sefaria.api.authenticate(formState, authMode);
if (!errors) { errors = {}; }
setErrors(errors);
setIsLoading(false);
if (Object.keys(errors).length === 0 && Sefaria._auth.uid) {
onLoginSuccess();
}
};
const getMobileAppKey = async () => {
remoteConfig().setDefaults({ mobile_app_key: '' });
await remoteConfig().fetch(0);
const activated = await remoteConfig().activate();
//if (!activated) { console.log('Fetch data not activated'); return ''; } I may have misunderstood what activated meant. but we shouldn't return '' if it's false
const snapshot = await remoteConfig().getValue('mobile_app_key');
return snapshot.asString();
};
const useAuthForm = (authMode, onLoginSuccess) => {
const [first_name, setFirstName] = useState(null);
const [last_name, setLastName] = useState(null);
const [email, setEmail] = useState(null);
const [password, setPassword] = useState(null);
const [errors, setErrors] = useState({});
const [isLoading, setIsLoading] = useState(false);
const formState = {
first_name,
last_name,
email,
password,
};
return {
errors,
setFirstName,
setLastName,
setEmail,
setPassword,
isLoading,
onSubmit: () => { onSubmit(formState, authMode, setErrors, onLoginSuccess, setIsLoading) },
}
}
const AuthPage = ({ authMode, close, showToast, openLogin, openRegister, openUri, syncProfile }) => {
const dispatch = useContext(DispatchContext);
const { themeStr, interfaceLanguage } = useContext(GlobalStateContext);
const {
errors,
setFirstName,
setLastName,
setEmail,
setPassword,
isLoading,
onSubmit,
} = useAuthForm(authMode, async () => {
dispatch({
type: STATE_ACTIONS.setIsLoggedIn,
value: true,
});
// try to sync immediately after login
syncProfile();
close(authMode);
showToast(strings.loginSuccessful);
});
const theme = getTheme(themeStr);
const isLogin = authMode === 'login';
const placeholderTextColor = themeStr == "black" ? "#BBB" : "#777";
const isHeb = interfaceLanguage === 'hebrew';
const mainContent = (
<ScrollView style={{flex:1, alignSelf: "stretch"}} contentContainerStyle={{alignItems: "center", paddingBottom: 50}} keyboardShouldPersistTaps='handled'>
<RainbowBar />
<View style={{ flex: 1, alignSelf: "stretch", alignItems: "flex-end", marginHorizontal: 10}}>
<CircleCloseButton onPress={close} />
</View>
<Text style={[styles.pageTitle, theme.text]}>{isLogin ? strings.log_in : strings.sign_up}</Text>
<View style={{flex: 1, alignSelf: "stretch", marginHorizontal: 37}}>
<View style={styles.logInMotivator}>
{
[
{iconStr: 'star', text: strings.saveTexts},
{iconStr: 'sync', text: strings.syncYourReading},
{iconStr: 'sheet', text: strings.readYourSheets},
{iconStr: 'mail', text: strings.getUpdates},
].map(x => (<LogInMotivator key={x.iconStr} { ...x } />))
}
</View>
{ isLogin ? null :
<AuthTextInput
placeholder={strings.first_name}
placeholderTextColor={placeholderTextColor}
error={errors.first_name}
errorText={errors.first_name}
onChangeText={setFirstName}
/>
}
{ isLogin ? null :
<AuthTextInput
placeholder={strings.last_name}
placeholderTextColor={placeholderTextColor}
error={errors.last_name}
errorText={errors.last_name}
onChangeText={setLastName}
/>
}
<AuthTextInput
placeholder={strings.email}
autoCapitalize={'none'}
placeholderTextColor={placeholderTextColor}
error={errors.username || errors.email}
errorText={errors.username || errors.email}
onChangeText={setEmail}
/>
<AuthTextInput
placeholder={strings.password}
placeholderTextColor={placeholderTextColor}
isPW={true}
error={errors.password || errors.password1}
errorText={errors.password || errors.password1}
onChangeText={setPassword}
/>
<ErrorText error={errors.non_field_errors} errorText={errors.non_field_errors} />
<SystemButton
isLoading={isLoading}
onPress={onSubmit}
text={isLogin ? strings.log_in : strings.sign_up}
isHeb={isHeb}
isBlue
/>
{
isLogin ?
<View style={{ alignItems: 'center', marginTop: 15 }}>
<View style={{flexDirection: isHeb ? 'row-reverse' : 'row', alignItems: 'center'}}>
<Text style={[theme.secondaryText, isHeb ? styles.heInt : styles.enInt]}>{strings.dontHaveAnAccount}</Text>
<TouchableOpacity onPress={openRegister}>
<Text style={[theme.text, isHeb ? styles.heInt : styles.enInt]}>{` ${strings.createAnAccount}`}</Text>
</TouchableOpacity>
</View>
<TouchableOpacity onPress={() => { openUri('https://www.sefaria.org/password/reset')}}>
<Text style={[theme.text, isHeb ? styles.heInt : styles.enInt]}>{strings.forgotPassword}</Text>
</TouchableOpacity>
</View>
:
<View style={{alignItems: 'center', marginTop: 15}}>
<View style={{flexDirection: isHeb ? 'row-reverse' : 'row', alignItems: 'center'}}>
<Text style={[theme.secondaryText, isHeb ? styles.heInt : styles.enInt]}>{strings.alreadyHaveAnAccount}</Text>
<TouchableOpacity onPress={openLogin}>
<Text style={[theme.text, isHeb ? styles.heInt : styles.enInt]}>{` ${strings.log_in}.`}</Text>
</TouchableOpacity>
</View>
<View style={{alignItems: 'center'}}>
<Text style={[theme.secondaryText, isHeb ? styles.heInt : styles.enInt]}>{strings.byClickingSignUp}</Text>
<TouchableOpacity onPress={() => { openUri('https://www.sefaria.org/terms')}}>
<Text style={[theme.text, isHeb ? styles.heInt : styles.enInt]}>{` ${strings.termsOfUseAndPrivacyPolicy}.`}</Text>
</TouchableOpacity>
</View>
</View>
}
</View>
</ScrollView>
)
return(
Platform.OS == "ios" ?
<KeyboardAvoidingView style={{flex:1, alignSelf: "stretch"}} contentContainerStyle={{alignItems: "center", paddingBottom: 50}} behavior="padding">
{mainContent}
</KeyboardAvoidingView>
:
<View style={{flex:1, alignSelf: "stretch"}} contentContainerStyle={{alignItems: "center", paddingBottom: 50}}>
{mainContent}
</View>
)
}
AuthPage.propTypes = {
authMode: PropTypes.string.isRequired,
close: PropTypes.func.isRequired,
showToast:PropTypes.func.isRequired,
openLogin: PropTypes.func.isRequired,
openRegister: PropTypes.func.isRequired,
openUri: PropTypes.func.isRequired,
};
const ErrorText = ({ error, errorText }) => (
error ?
(
<Text>
{ errorText }
</Text>
) : null
);
const AuthTextInput = ({
isPW,
placeholder,
placeholderTextColor,
autoCapitalize,
error,
errorText,
onChangeText,
}) => (
<GlobalStateContext.Consumer>
{
({ themeStr, interfaceLanguage }) => (
<View>
<TextInput
style={[
styles.textInput,
styles.systemButton,
styles.boxShadow,
styles.authTextInput,
interfaceLanguage === 'hebrew' ? styles.heInt : styles.enInt,
getTheme(themeStr).text,
getTheme(themeStr).mainTextPanel
]}
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
secureTextEntry={isPW}
autoCapitalize={autoCapitalize}
onChangeText={onChangeText}
/>
<ErrorText error={error} errorText={errorText} />
</View>
)
}
</GlobalStateContext.Consumer>
);
const LogInMotivator = ({
iconStr,
text
}) => {
const { themeStr, interfaceLanguage } = useContext(GlobalStateContext);
const theme = getTheme(themeStr);
const isHeb = interfaceLanguage === 'hebrew';
let icon;
if (themeStr === 'white') {
if (iconStr === 'star') { icon = require('./img/starUnfilled.png'); }
if (iconStr === 'sync') { icon = require('./img/sync.png'); }
if (iconStr === 'sheet') { icon = require('./img/sheet.png'); }
if (iconStr === 'mail') { icon = require('./img/mail.png'); }
} else {
if (iconStr === 'star') { icon = require('./img/starUnfilled-light.png'); }
if (iconStr === 'sync') { icon = require('./img/sync-light.png'); }
if (iconStr === 'sheet') { icon = require('./img/sheet-light.png'); }
if (iconStr === 'mail') { icon = require('./img/mail-light.png'); }
}
return (
<View style={{
flexDirection: isHeb ? 'row-reverse' : 'row',
marginBottom: 20,
}}
>
<Image style={{height: 20, width: 20}} source={icon} resizeMode={'contain'} />
<Text style={[{marginHorizontal: 20, fontSize: 16}, isHeb ? styles.heInt : styles.enInt, theme.text]}>{ text }</Text>
</View>
);
}
export {
AuthPage,
AuthTextInput,
};