Files
notesnook/apps/mobile/src/components/LoginDialog/index.js

950 lines
30 KiB
JavaScript
Raw Normal View History

2020-12-13 14:48:17 +05:00
import React, { useEffect, useRef, useState } from 'react';
import { ActivityIndicator, Modal, TouchableOpacity, View } from 'react-native';
import { TextInput } from 'react-native-gesture-handler';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2020-09-14 11:43:07 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-12-13 14:48:17 +05:00
import { Button } from '../../components/Button';
2020-09-14 11:43:07 +05:00
import Seperator from '../../components/Seperator';
2020-12-13 14:48:17 +05:00
import { Toast } from '../../components/Toast';
import { Actions } from '../../provider/Actions';
import { useTracked } from '../../provider/index';
import { DDS } from '../../services/DeviceDetection';
2020-09-14 12:33:04 +05:00
import {
2020-10-26 12:01:05 +05:00
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
2020-12-13 14:48:17 +05:00
ToastEvent
2020-10-13 17:02:14 +05:00
} from '../../services/EventManager';
2020-12-13 14:48:17 +05:00
import { clearMessage } from '../../services/Message';
2020-09-14 11:43:07 +05:00
import {
validateEmail,
validatePass,
2020-12-13 14:48:17 +05:00
validateUsername
2020-10-13 17:02:14 +05:00
} from '../../services/Validation';
2020-12-13 14:48:17 +05:00
import { getElevation } from '../../utils';
import { db } from '../../utils/DB';
2020-11-02 20:50:07 +05:00
import {
eOpenLoginDialog,
eOpenRecoveryKeyDialog,
eStartSyncer,
2020-12-13 14:48:17 +05:00
refreshNotesPage
2020-11-02 20:50:07 +05:00
} from '../../utils/Events';
2020-12-13 14:48:17 +05:00
import { SIZE, WEIGHT } from '../../utils/SizeUtils';
import { sleep } from '../../utils/TimeUtils';
import { ActionIcon } from '../ActionIcon';
2020-12-09 13:11:21 +05:00
import BaseDialog from '../Dialog/base-dialog';
import DialogContainer from '../Dialog/dialog-container';
2020-12-13 14:48:17 +05:00
import { ListHeaderComponent } from '../SimpleList/ListHeaderComponent';
2020-12-09 13:11:21 +05:00
import Heading from '../Typography/Heading';
2020-11-14 11:07:01 +05:00
import Paragraph from '../Typography/Paragraph';
2020-01-23 23:18:15 +05:00
2020-12-13 14:48:17 +05:00
const MODES = {
login: 0,
signup: 1,
forgotPassword: 2,
changePassword: 3,
};
2020-09-14 11:43:07 +05:00
const LoginDialog = () => {
const [state, dispatch] = useTracked();
const colors = state.colors;
2020-09-14 12:33:04 +05:00
const [visible, setVisible] = useState(false);
2020-12-09 13:11:21 +05:00
const [status, setStatus] = useState(null);
2020-09-14 11:43:07 +05:00
const [loggingIn, setLoggingIn] = useState(false);
const [email, setEmail] = useState(null);
const [password, setPassword] = useState(null);
const [invalidEmail, setInvalidEmail] = useState(false);
const [invalidPassword, setInvalidPassword] = useState(false);
const [username, setUsername] = useState(null);
const [invalidUsername, setInvalidUsername] = useState(false);
const [secureEntry, setSecureEntry] = useState(true);
const [confirmPassword, setConfirmPassword] = useState(false);
2020-12-13 14:48:17 +05:00
const [oldPassword, setOldPassword] = useState(null);
2020-09-14 11:43:07 +05:00
const [passwordReEnter, setPasswordReEnter] = useState(null);
const [failed, setFailed] = useState(false);
const [signingIn, setSigningIn] = useState(false);
const [userConsent, setUserConsent] = useState(false);
const [mode, setMode] = useState(MODES.login);
2020-12-13 14:48:17 +05:00
const _email = useRef();
const _pass = useRef();
const _username = useRef();
const _oldPass = useRef();
const _oPass = useRef();
const _passConfirm = useRef();
const _passContainer = useRef();
2020-09-14 12:33:04 +05:00
const insets = useSafeAreaInsets();
2020-12-13 14:48:17 +05:00
const MODE_DATA = [
{
headerButton: 'Login',
headerButtonFunc: () => {
setMode(MODES.signup);
},
button: 'Login',
buttonFunc: loginUser,
headerParagraph: 'create a new account',
showForgotButton: true,
loading: 'Please wait while we log in and sync your data.',
showLoader: true,
},
{
headerButton: 'Sign Up',
headerButtonFunc: () => {
setMode(MODES.login);
},
button: 'Create Account',
buttonFunc: signupUser,
headerParagraph: 'login to your account',
showForgotButton: false,
loading: 'Please wait while we are setting up your account.',
showLoader: true,
},
{
headerButton: 'Forgot Password',
headerButtonFunc: () => {
setMode(MODES.signup);
},
button: 'Send Recovery Email',
buttonFunc: sendEmail,
headerParagraph: 'login to your account',
showForgotButton: false,
loading:
'We have sent you a recovery email on ' +
email +
'. Follow the link in the email to set a new password',
showLoader: false,
},
{
headerButton: 'Change Password',
headerButtonFunc: () => {
setMode(MODES.signup);
},
button: 'Change Password',
buttonFunc: changePassword,
headerParagraph: 'login to your account',
showForgotButton: false,
loading:
'Please wait while we change your password and encrypt your data.',
showLoader: true,
},
];
const current = MODE_DATA[mode];
2020-09-14 12:33:04 +05:00
useEffect(() => {
eSubscribeEvent(eOpenLoginDialog, open);
return () => {
eUnSubscribeEvent(eOpenLoginDialog, open);
};
}, []);
2020-01-23 23:18:15 +05:00
2020-12-13 14:48:17 +05:00
function open(mode) {
setMode(mode? mode : MODES.login);
2020-09-14 11:43:07 +05:00
setVisible(true);
2020-01-23 23:18:15 +05:00
}
2020-09-14 11:43:07 +05:00
const close = () => {
2020-11-20 01:21:58 +05:00
if (loggingIn || signingIn) return;
2020-09-27 10:15:19 +05:00
_email.current?.clear();
_pass.current?.clear();
_passConfirm.current?.clear();
_username.current?.clear();
2020-09-14 11:43:07 +05:00
setVisible(false);
2020-09-27 10:15:19 +05:00
setUsername(null);
setPassword(null);
setConfirmPassword(null);
setUserConsent(false);
2020-12-13 14:48:17 +05:00
setEmail(null);
2020-09-27 10:15:19 +05:00
setLoggingIn(false);
2020-12-13 14:48:17 +05:00
setMode(MODES.login);
2020-09-14 11:43:07 +05:00
};
2020-01-23 23:18:15 +05:00
2020-09-14 11:43:07 +05:00
const loginUser = async () => {
if (
!password ||
password.length < 8 ||
!username ||
invalidPassword ||
invalidUsername
) {
2020-09-14 13:34:55 +05:00
ToastEvent.show('username or password is invalid', 'error', 'local');
2020-09-14 11:43:07 +05:00
return;
}
2020-09-14 13:34:55 +05:00
2020-09-14 11:43:07 +05:00
setLoggingIn(true);
_username.current.blur();
_pass.current.blur();
2020-09-24 09:42:12 +05:00
setStatus('Logging in');
2020-09-14 11:43:07 +05:00
try {
2020-12-10 13:06:59 +05:00
await db.user.login(username.toLowerCase(), password, true);
2020-09-14 11:43:07 +05:00
} catch (e) {
2020-12-09 13:11:21 +05:00
ToastEvent.show(e.message, 'error', 'local');
setLoggingIn(false);
2020-09-14 11:43:07 +05:00
return;
}
try {
2020-09-24 09:42:12 +05:00
let user = await db.user.get();
2020-10-28 15:52:15 +05:00
if (!user) throw new Error('Username or passoword incorrect!');
2020-12-09 13:11:21 +05:00
setStatus('Syncing Data');
2020-10-13 17:02:14 +05:00
dispatch({type: Actions.USER, user: user});
2020-09-14 11:43:07 +05:00
await db.sync();
eSendEvent(eStartSyncer);
2020-10-13 17:02:14 +05:00
dispatch({type: Actions.ALL});
2020-09-14 11:43:07 +05:00
eSendEvent(refreshNotesPage);
2020-12-10 13:06:59 +05:00
clearMessage(dispatch);
2020-11-02 20:50:07 +05:00
close();
2020-09-14 13:34:55 +05:00
ToastEvent.show(`Logged in as ${username}`, 'success', 'local');
2020-09-14 11:43:07 +05:00
} catch (e) {
2020-12-10 13:06:59 +05:00
console.warn(e);
2020-09-24 09:42:12 +05:00
ToastEvent.show(e.message, 'error', 'local');
} finally {
2020-09-14 11:43:07 +05:00
setLoggingIn(false);
}
};
2020-09-14 12:43:15 +05:00
const validateInfo = () => {
if (!password || !email || !username || !passwordReEnter) {
ToastEvent.show('All fields are required', 'error', 'local');
return false;
}
if (!confirmPassword) {
ToastEvent.show('Passwords do not match', 'error', 'local');
return false;
}
2020-11-23 12:32:33 +05:00
2020-10-28 11:26:49 +05:00
if (invalidEmail && invalidPassword && invalidUsername) {
2020-09-14 12:43:15 +05:00
ToastEvent.show('Signup information is invalid', 'error', 'local');
return false;
}
if (!userConsent) {
ToastEvent.show(
'You must agree to our terms of service and privacy policy.',
'error',
'local',
5000,
() => {
setUserConsent(true);
signupUser();
ToastEvent.hide();
},
'I Agree',
);
return false;
}
2020-10-28 11:26:49 +05:00
return true;
2020-09-14 12:43:15 +05:00
};
2020-09-14 11:43:07 +05:00
const signupUser = async () => {
2020-10-26 12:01:05 +05:00
if (!validateInfo()) return;
2020-09-14 11:43:07 +05:00
setSigningIn(true);
2020-12-09 13:11:21 +05:00
setStatus('Creating User');
2020-09-14 11:43:07 +05:00
try {
await db.user.signup(username, email, password);
} catch (e) {
setSigningIn(false);
setFailed(true);
ToastEvent.show('Signup failed, Network Error', 'error', 'local');
return;
}
let user;
try {
2020-09-24 09:17:14 +05:00
user = await db.user.get();
2020-09-14 12:33:04 +05:00
setStatus('Setting up crenditials');
2020-10-13 17:02:14 +05:00
dispatch({type: Actions.USER, user: user});
2020-09-14 11:43:07 +05:00
eSendEvent(eStartSyncer);
2020-12-10 13:06:59 +05:00
clearMessage(dispatch);
2020-10-28 11:26:49 +05:00
close();
await sleep(500);
2020-10-28 15:52:15 +05:00
eSendEvent(eOpenRecoveryKeyDialog, true);
2020-09-14 11:43:07 +05:00
} catch (e) {
setFailed(true);
ToastEvent.show('Login Failed, try again', 'error', 'local');
2020-10-28 11:26:49 +05:00
} finally {
setSigningIn(false);
2020-09-14 11:43:07 +05:00
}
};
2020-12-13 14:48:17 +05:00
const sendEmail = () => {
setStatus('Recovery Email Sent!');
// handle recovery email sending
};
const changePassword = () => {};
2020-11-23 12:32:33 +05:00
return !visible ? null : (
2020-09-14 11:43:07 +05:00
<Modal
animated={true}
animationType={DDS.isLargeTablet() ? 'fade' : 'slide'}
2020-09-14 12:33:04 +05:00
statusBarTranslucent={true}
2020-09-14 11:43:07 +05:00
onRequestClose={close}
2020-11-23 12:32:33 +05:00
visible={true}
2020-09-14 11:43:07 +05:00
transparent={true}>
2020-12-13 14:48:17 +05:00
{status ? (
<BaseDialog
visible={true}
onRequestClose={() => {
if (!current.showLoader) {
setStatus(null);
}
}}>
2020-12-09 13:11:21 +05:00
<DialogContainer>
<View
style={{
justifyContent: 'center',
alignItems: 'center',
marginBottom: 10,
}}>
<Heading>{status}</Heading>
<Paragraph style={{textAlign: 'center'}}>
2020-12-13 14:48:17 +05:00
{current.loading}
{!current.showLoader ? null : (
<Paragraph color={colors.errorText}>
{' '}
Do not close the app.
</Paragraph>
)}
2020-12-09 13:11:21 +05:00
</Paragraph>
</View>
2020-12-13 14:48:17 +05:00
{!current.showLoader ? null : (
<ActivityIndicator color={colors.accent} />
)}
2020-12-09 13:11:21 +05:00
</DialogContainer>
</BaseDialog>
) : null}
2020-09-14 12:33:04 +05:00
<View
2020-09-14 11:43:07 +05:00
style={{
2020-09-14 12:33:04 +05:00
opacity: 1,
2020-09-14 11:43:07 +05:00
flex: 1,
2020-12-13 14:48:17 +05:00
paddingTop: DDS.isLargeTablet() ? 0 : insets.top,
backgroundColor: DDS.isLargeTablet() ? 'rgba(0,0,0,0.3)' : colors.bg,
2020-09-14 11:43:07 +05:00
width: '100%',
height: '100%',
alignSelf: 'center',
justifyContent: 'center',
alignItems: 'center',
2020-10-28 11:26:49 +05:00
overflow: 'hidden',
2020-09-14 11:43:07 +05:00
}}>
2020-12-13 14:48:17 +05:00
{DDS.isLargeTablet() ? (
2020-01-23 23:18:15 +05:00
<TouchableOpacity
2020-09-14 11:43:07 +05:00
onPress={close}
2020-01-23 23:18:15 +05:00
style={{
width: '100%',
height: '100%',
position: 'absolute',
zIndex: 1,
}}
/>
2020-09-14 11:43:07 +05:00
) : null}
<View
style={{
2020-11-20 01:21:58 +05:00
maxHeight: DDS.isLargeTablet() ? '90%' : '100%',
minHeight: '50%',
height: DDS.isLargeTablet() ? null : '100%',
2020-12-13 14:48:17 +05:00
width: DDS.isLargeTablet() ? 500 : '100%',
borderRadius: DDS.isLargeTablet() ? 5 : 0,
2020-11-20 01:21:58 +05:00
backgroundColor: colors.bg,
zIndex: 10,
2020-12-13 14:48:17 +05:00
...getElevation(DDS.isLargeTablet() ? 5 : 0),
2020-11-20 01:21:58 +05:00
paddingBottom: DDS.isLargeTablet() ? 20 : 0,
2020-09-14 11:43:07 +05:00
}}>
2020-09-14 13:34:55 +05:00
<Toast context="local" />
2020-10-26 12:01:05 +05:00
2020-11-20 01:21:58 +05:00
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
paddingHorizontal: 12,
height: 50,
}}>
{DDS.isLargeTablet() ? (
<View />
) : (
2020-11-14 11:07:01 +05:00
<ActionIcon
name="arrow-left"
size={SIZE.xxxl}
onPress={() => {
close();
}}
customStyle={{
width: 40,
height: 40,
marginLeft: -5,
}}
color={colors.heading}
/>
2020-11-20 01:21:58 +05:00
)}
2020-12-13 14:48:17 +05:00
<View />
2020-11-20 01:21:58 +05:00
</View>
2020-10-28 11:26:49 +05:00
2020-11-10 17:17:39 +05:00
<ListHeaderComponent
color="transparent"
type="settings"
2020-11-20 01:21:58 +05:00
shouldShow
2020-12-13 14:48:17 +05:00
title={current.headerButton}
2020-11-10 17:17:39 +05:00
messageCard={false}
2020-12-13 14:48:17 +05:00
onPress={mode !== MODES.changePassword && current.headerButtonFunc}
paragraph={mode !== MODES.changePassword && current.headerParagraph}
2020-11-10 17:17:39 +05:00
/>
2020-10-28 11:26:49 +05:00
<View
style={{
paddingHorizontal: 12,
2020-11-10 17:17:39 +05:00
paddingTop: 12,
2020-10-28 11:26:49 +05:00
}}>
2020-12-13 14:48:17 +05:00
{mode === MODES.forgotPassword ||
mode === MODES.changePassword ? null : (
<>
<TextInput
ref={_username}
onFocus={() => {
if (!invalidUsername) {
_username.current?.setNativeProps({
style: {
borderColor: colors.accent,
},
});
}
}}
editable={!loggingIn || !signingIn}
autoCapitalize="none"
defaultValue={username}
onBlur={() => {
if (!validateUsername(username) && username?.length > 0) {
setInvalidUsername(true);
_username.current?.setNativeProps({
style: {
color: colors.errorText,
borderColor: colors.errorText,
},
});
} else {
setInvalidUsername(false);
_username.current?.setNativeProps({
style: {
borderColor: colors.nav,
},
});
}
}}
textContentType="username"
onChangeText={(value) => {
setUsername(value);
2020-10-28 11:26:49 +05:00
2020-12-13 14:48:17 +05:00
if (invalidUsername && validateUsername(username)) {
setInvalidUsername(false);
_username.current.setNativeProps({
style: {
color: colors.pri,
borderColor: colors.accent,
},
});
}
}}
onSubmitEditing={() => {
if (!validateUsername(username)) {
setInvalidUsername(true);
_username.current.setNativeProps({
style: {
color: colors.errorText,
},
});
}
_pass.current?.focus();
}}
blurOnSubmit={false}
2020-09-14 11:43:07 +05:00
style={{
2020-12-13 14:48:17 +05:00
paddingHorizontal: 0,
height: 50,
borderBottomWidth: 1,
borderColor: colors.nav,
fontSize: SIZE.md,
fontFamily: WEIGHT.regular,
color: colors.pri,
}}
placeholder="Username (a-z _- 0-9)"
placeholderTextColor={colors.icon}
/>
{invalidUsername ? (
<Paragraph
2020-10-28 11:26:49 +05:00
size={SIZE.xs}
2020-12-13 14:48:17 +05:00
style={{
textAlign: 'right',
textAlignVertical: 'bottom',
marginTop: 2.5,
}}>
<Icon
name="alert-circle-outline"
size={SIZE.xs}
color={colors.errorText}
/>{' '}
Username is invalid
</Paragraph>
) : null}
</>
)}
2020-10-28 11:26:49 +05:00
<Seperator />
2020-12-13 14:48:17 +05:00
{mode !== MODES.signup ? null : (
2020-10-28 11:26:49 +05:00
<>
<TextInput
ref={_email}
onFocus={() => {
if (!invalidEmail) {
_email.current.setNativeProps({
style: {
borderColor: colors.accent,
},
});
}
}}
2020-11-14 11:31:25 +05:00
editable={!loggingIn || !signingIn}
2020-10-28 11:26:49 +05:00
autoCapitalize="none"
defaultValue={email}
onBlur={() => {
if (!validateEmail(email) && email?.length > 0) {
setInvalidEmail(true);
2020-11-02 23:13:56 +05:00
_email.current?.setNativeProps({
2020-10-28 11:26:49 +05:00
style: {
color: colors.errorText,
borderColor: colors.errorText,
},
});
} else {
setInvalidEmail(false);
2020-11-02 23:13:56 +05:00
_email.current?.setNativeProps({
2020-10-28 11:26:49 +05:00
style: {
borderColor: colors.nav,
},
});
}
}}
textContentType="emailAddress"
onChangeText={(value) => {
setEmail(value);
if (invalidEmail && validateEmail(email)) {
setInvalidEmail(false);
_email.current.setNativeProps({
style: {
color: colors.pri,
borderColor: colors.accent,
},
});
}
}}
2020-12-13 14:48:17 +05:00
blurOnSubmit={false}
2020-10-28 11:26:49 +05:00
onSubmitEditing={() => {
if (!validateEmail(email)) {
setInvalidEmail(true);
_email.current.setNativeProps({
style: {
color: colors.errorText,
},
});
}
2020-12-13 14:48:17 +05:00
if (mode === MODES.signup) {
_pass.current?.focus();
}
}}
2020-10-28 11:26:49 +05:00
style={{
2020-12-13 14:48:17 +05:00
paddingHorizontal: 0,
2020-10-28 11:26:49 +05:00
height: 50,
2020-11-02 20:50:07 +05:00
borderBottomWidth: 1,
2020-10-28 11:26:49 +05:00
borderColor: colors.nav,
2020-11-14 11:09:06 +05:00
fontSize: SIZE.md,
2020-10-28 11:26:49 +05:00
fontFamily: WEIGHT.regular,
color: colors.pri,
}}
2020-12-13 14:48:17 +05:00
placeholder="youremail@gmail.com"
2020-10-28 11:26:49 +05:00
placeholderTextColor={colors.icon}
/>
2020-10-28 11:26:49 +05:00
{invalidEmail ? (
2020-11-20 01:21:58 +05:00
<Paragraph
size={SIZE.xs}
2020-09-14 11:43:07 +05:00
style={{
2020-10-28 11:26:49 +05:00
textAlign: 'right',
textAlignVertical: 'bottom',
marginTop: 2.5,
2020-09-14 11:43:07 +05:00
}}>
2020-10-28 11:26:49 +05:00
<Icon
name="alert-circle-outline"
size={SIZE.xs}
color={colors.errorText}
/>{' '}
Email is invalid
2020-11-20 01:21:58 +05:00
</Paragraph>
2020-10-28 11:26:49 +05:00
) : null}
2020-09-19 16:02:21 +05:00
<Seperator />
2020-10-28 11:26:49 +05:00
</>
)}
2020-09-19 16:02:21 +05:00
2020-12-13 14:48:17 +05:00
{mode !== MODES.changePassword ? null : (
<>
<View
ref={_oldPass}
style={{
borderBottomWidth: 1,
borderColor: colors.nav,
paddingHorizontal: 0,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}}>
<TextInput
ref={_oPass}
editable={!loggingIn || !signingIn}
autoCapitalize="none"
defaultValue={oldPassword}
onChangeText={(value) => {
setOldPassword(value);
}}
onSubmitEditing={() => {
if (mode === MODES.changePassword) {
_pass.current?.focus();
}
}}
style={{
paddingHorizontal: 0,
height: 50,
fontSize: SIZE.md,
fontFamily: WEIGHT.regular,
width: '85%',
maxWidth: '85%',
color: colors.pri,
}}
secureTextEntry={secureEntry}
placeholder="Current Password"
placeholderTextColor={colors.icon}
/>
2020-10-28 11:26:49 +05:00
2020-12-13 14:48:17 +05:00
<Icon
name="eye"
size={20}
onPress={() => {
setSecureEntry(!secureEntry);
}}
style={{
width: 25,
}}
color={secureEntry ? colors.icon : colors.accent}
/>
</View>
2020-10-28 11:26:49 +05:00
2020-12-13 14:48:17 +05:00
{invalidPassword ? (
<Paragraph
size={SIZE.xs}
style={{
textAlign: 'right',
textAlignVertical: 'bottom',
marginTop: 2.5,
}}>
<Icon
name="alert-circle-outline"
size={SIZE.xs}
color={colors.errorText}
/>{' '}
Password is invalid
</Paragraph>
) : null}
2020-10-28 11:26:49 +05:00
2020-12-13 14:48:17 +05:00
<Seperator />
</>
)}
{mode === MODES.forgotPassword ? null : (
<>
<View
ref={_passContainer}
style={{
borderBottomWidth: 1,
borderColor: colors.nav,
paddingHorizontal: 0,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}}>
<TextInput
ref={_pass}
onFocus={() => {
if (!invalidPassword) {
_passContainer.current?.setNativeProps({
style: {
borderColor: colors.accent,
},
});
}
}}
editable={!loggingIn || !signingIn}
autoCapitalize="none"
defaultValue={password}
onBlur={() => {
if (!validatePass(password) && password?.length > 0) {
setInvalidPassword(true);
_pass.current?.setNativeProps({
style: {
color: colors.errorText,
},
});
_passContainer.current?.setNativeProps({
style: {
borderColor: colors.errorText,
},
});
} else {
setInvalidPassword(false);
_passContainer.current?.setNativeProps({
style: {
borderColor: colors.nav,
},
});
}
}}
onChangeText={(value) => {
setPassword(value);
if (invalidPassword && validatePass(password)) {
setInvalidPassword(false);
_pass.current.setNativeProps({
style: {
color: colors.pri,
},
});
}
}}
onSubmitEditing={() => {
if (!validatePass(password)) {
setInvalidPassword(true);
_pass.current.setNativeProps({
style: {
color: colors.errorText,
},
});
}
if (
mode === MODES.signup ||
mode === MODES.changePassword
) {
_passConfirm.current?.focus();
} else {
current.buttonFunc();
}
}}
blurOnSubmit={false}
style={{
paddingHorizontal: 0,
height: 50,
fontSize: SIZE.md,
fontFamily: WEIGHT.regular,
width: '85%',
maxWidth: '85%',
color: colors.pri,
}}
secureTextEntry={secureEntry}
placeholder="Password (6+ characters)"
placeholderTextColor={colors.icon}
/>
<Icon
name="eye"
size={20}
onPress={() => {
setSecureEntry(!secureEntry);
}}
style={{
width: 25,
}}
color={secureEntry ? colors.icon : colors.accent}
/>
</View>
{mode === MODES.login ? (
<TouchableOpacity
onPress={() => {
setMode(MODES.forgotPassword);
}}
style={{
alignSelf: 'flex-end',
marginTop: 2.5,
}}>
<Paragraph color={colors.accent}>
Forgot password?
</Paragraph>
</TouchableOpacity>
) : null}
{invalidPassword ? (
<Paragraph
size={SIZE.xs}
style={{
textAlign: 'right',
textAlignVertical: 'bottom',
marginTop: 2.5,
}}>
<Icon
name="alert-circle-outline"
size={SIZE.xs}
color={colors.errorText}
/>{' '}
Password is invalid
</Paragraph>
) : null}
<Seperator />
</>
)}
{mode !== MODES.signup && mode !== MODES.changePassword ? null : (
2020-10-28 11:26:49 +05:00
<>
<TextInput
ref={_passConfirm}
2020-11-14 11:31:25 +05:00
editable={
!loggingIn || !signingIn || (password && !invalidPassword)
? true
: false
}
2020-10-28 11:26:49 +05:00
defaultValue={passwordReEnter}
autoCapitalize="none"
onChangeText={(value) => {
setPasswordReEnter(value);
if (value !== password) {
setConfirmPassword(false);
_passConfirm.current.setNativeProps({
style: {
borderColor: colors.errorText,
},
});
_pass.current.setNativeProps({
style: {
borderColor: colors.errorText,
},
});
} else {
setConfirmPassword(true);
_passConfirm.current.setNativeProps({
style: {
borderColor: colors.accent,
},
});
_pass.current.setNativeProps({
style: {
borderColor: colors.accent,
},
});
}
}}
onFocus={() => {
_passConfirm.current.setNativeProps({
style: {
borderColor: colors.accent,
},
});
}}
2020-12-13 14:48:17 +05:00
onSubmitEditing={() => {
current.buttonFunc();
}}
blurOnSubmit
2020-09-14 12:17:06 +05:00
style={{
2020-12-13 14:48:17 +05:00
paddingHorizontal: 0,
2020-11-02 20:50:07 +05:00
borderBottomWidth: 1,
2020-10-28 11:26:49 +05:00
height: 50,
2020-09-14 12:17:06 +05:00
borderColor: colors.nav,
2020-11-14 11:09:06 +05:00
fontSize: SIZE.md,
2020-10-28 11:26:49 +05:00
fontFamily: WEIGHT.regular,
color: colors.pri,
}}
secureTextEntry={secureEntry}
placeholder="Confirm Password"
placeholderTextColor={colors.icon}
/>
2020-09-14 11:43:07 +05:00
2020-10-28 11:26:49 +05:00
{password && !invalidPassword && !confirmPassword ? (
2020-11-20 01:21:58 +05:00
<Paragraph
size={SIZE.xs}
2020-09-14 12:17:06 +05:00
style={{
textAlign: 'right',
textAlignVertical: 'bottom',
marginTop: 2.5,
}}>
<Icon
name="alert-circle-outline"
size={SIZE.xs}
color={colors.errorText}
/>{' '}
2020-10-28 11:26:49 +05:00
Passwords do not match
2020-11-20 01:21:58 +05:00
</Paragraph>
2020-09-14 12:17:06 +05:00
) : null}
2020-12-13 14:48:17 +05:00
<Seperator />
2020-10-28 11:26:49 +05:00
</>
)}
2020-11-02 20:50:07 +05:00
2020-12-13 14:48:17 +05:00
{mode !== MODES.signup ? null : (
2020-11-02 20:50:07 +05:00
<>
<TouchableOpacity
2020-11-14 11:31:25 +05:00
disabled={loggingIn || signingIn}
2020-11-02 20:50:07 +05:00
onPress={() => {
setUserConsent(!userConsent);
2020-09-14 12:17:06 +05:00
}}
2020-11-02 20:50:07 +05:00
activeOpacity={0.7}
2020-09-14 12:17:06 +05:00
style={{
2020-11-02 20:50:07 +05:00
flexDirection: 'row',
width: '100%',
alignItems: 'center',
height: 40,
2020-09-14 12:17:06 +05:00
}}>
2020-11-02 20:50:07 +05:00
<Icon
size={SIZE.lg}
color={userConsent ? colors.accent : colors.icon}
name={
userConsent
? 'check-circle-outline'
: 'checkbox-blank-circle-outline'
}
/>
2020-11-20 01:21:58 +05:00
<Paragraph
2020-10-28 11:26:49 +05:00
style={{
2020-11-02 20:50:07 +05:00
maxWidth: '90%',
marginLeft: 10,
2020-09-14 11:43:07 +05:00
}}>
2020-11-02 20:50:07 +05:00
By signing up you agree to our{' '}
2020-11-20 01:21:58 +05:00
<Paragraph color={colors.accent}>
2020-11-02 20:50:07 +05:00
terms of service{' '}
2020-11-20 01:21:58 +05:00
</Paragraph>
2020-11-02 20:50:07 +05:00
and{' '}
2020-11-20 01:21:58 +05:00
<Paragraph color={colors.accent}>privacy policy.</Paragraph>
</Paragraph>
2020-11-02 20:50:07 +05:00
</TouchableOpacity>
</>
2020-10-28 11:26:49 +05:00
)}
2020-11-02 20:50:07 +05:00
2020-12-13 14:48:17 +05:00
{mode !== MODES.signup ? null : <Seperator />}
2020-10-28 11:26:49 +05:00
2020-11-20 01:21:58 +05:00
<Button
2020-12-13 14:48:17 +05:00
title={current.button}
onPress={current.buttonFunc}
2020-11-20 01:21:58 +05:00
width="100%"
type="accent"
loading={loggingIn || signingIn}
fontSize={SIZE.md}
height={50}
/>
2020-10-28 11:26:49 +05:00
</View>
2020-09-14 11:43:07 +05:00
</View>
2020-09-14 12:33:04 +05:00
</View>
2020-09-14 11:43:07 +05:00
</Modal>
);
};
2020-01-23 23:18:15 +05:00
export default LoginDialog;