2022-02-07 14:44:48 +05:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2022-02-18 14:45:38 +05:00
|
|
|
import { Platform, View } from 'react-native';
|
2022-02-11 10:38:14 +05:00
|
|
|
import { SheetManager } from 'react-native-actions-sheet';
|
2022-02-14 14:54:15 +05:00
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
2022-02-28 23:25:18 +05:00
|
|
|
import { DDS } from '../../services/device-detection';
|
2022-04-24 05:59:14 +05:00
|
|
|
import { eSendEvent, ToastEvent } from '../../services/event-manager';
|
2022-02-28 23:25:18 +05:00
|
|
|
import { clearMessage } from '../../services/message';
|
|
|
|
|
import PremiumService from '../../services/premium';
|
2022-03-23 09:54:32 +05:00
|
|
|
import SettingsService from '../../services/settings';
|
2022-04-24 05:59:14 +05:00
|
|
|
import { useUserStore } from '../../stores/use-user-store';
|
|
|
|
|
import { useThemeStore } from '../../stores/use-theme-store';
|
2022-02-07 14:44:48 +05:00
|
|
|
import { db } from '../../utils/database';
|
2022-02-28 13:48:59 +05:00
|
|
|
import { eCloseLoginDialog } from '../../utils/events';
|
|
|
|
|
import { SIZE } from '../../utils/size';
|
|
|
|
|
import { sleep } from '../../utils/time';
|
|
|
|
|
import BaseDialog from '../dialog/base-dialog';
|
2022-03-23 09:54:32 +05:00
|
|
|
import SheetProvider from '../sheet-provider';
|
2022-04-24 05:59:14 +05:00
|
|
|
import { Progress } from '../sheets/progress';
|
2022-03-23 09:54:32 +05:00
|
|
|
import { Button } from '../ui/button';
|
|
|
|
|
import { IconButton } from '../ui/icon-button';
|
2022-02-28 13:48:59 +05:00
|
|
|
import Input from '../ui/input';
|
|
|
|
|
import { SvgView } from '../ui/svg';
|
|
|
|
|
import { BouncingView } from '../ui/transitions/bouncing-view';
|
|
|
|
|
import Heading from '../ui/typography/heading';
|
|
|
|
|
import Paragraph from '../ui/typography/paragraph';
|
2022-02-07 14:44:48 +05:00
|
|
|
import { SVG } from './background';
|
|
|
|
|
import { ForgotPassword } from './forgot-password';
|
2022-03-18 14:40:15 +05:00
|
|
|
import TwoFactorVerification from './two-factor';
|
2022-02-07 14:44:48 +05:00
|
|
|
export const Login = ({ changeMode }) => {
|
2022-02-28 23:25:18 +05:00
|
|
|
const colors = useThemeStore(state => state.colors);
|
2022-02-07 14:44:48 +05:00
|
|
|
const email = useRef();
|
|
|
|
|
const emailInputRef = useRef();
|
|
|
|
|
const passwordInputRef = useRef();
|
|
|
|
|
const password = useRef();
|
|
|
|
|
const [focused, setFocused] = useState(false);
|
|
|
|
|
const [error, setError] = useState(false);
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2022-02-14 14:54:15 +05:00
|
|
|
const insets = useSafeAreaInsets();
|
2022-02-07 14:44:48 +05:00
|
|
|
|
|
|
|
|
const setUser = useUserStore(state => state.setUser);
|
|
|
|
|
|
|
|
|
|
const validateInfo = () => {
|
|
|
|
|
if (!password.current || !email.current) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'All fields required',
|
|
|
|
|
message: 'Fill all the fields and try again',
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
async () => {
|
|
|
|
|
await sleep(500);
|
|
|
|
|
emailInputRef.current?.focus();
|
|
|
|
|
setFocused(true);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-03-18 14:40:15 +05:00
|
|
|
const login = async (mfa, callback) => {
|
2022-02-07 14:44:48 +05:00
|
|
|
if (!validateInfo() || error) return;
|
|
|
|
|
setLoading(true);
|
|
|
|
|
let user;
|
2022-04-01 00:55:16 +05:00
|
|
|
console.log(mfa);
|
2022-02-07 14:44:48 +05:00
|
|
|
try {
|
2022-03-18 14:40:15 +05:00
|
|
|
if (mfa) {
|
|
|
|
|
await db.user.mfaLogin(email.current.toLowerCase(), password.current, mfa);
|
|
|
|
|
} else {
|
2022-04-01 00:55:16 +05:00
|
|
|
console.log(email.current, password.current);
|
2022-03-18 14:40:15 +05:00
|
|
|
await db.user.login(email.current.toLowerCase(), password.current);
|
|
|
|
|
}
|
|
|
|
|
callback && callback(true);
|
|
|
|
|
|
2022-02-07 14:44:48 +05:00
|
|
|
user = await db.user.getUser();
|
|
|
|
|
if (!user) throw new Error('Email or password incorrect!');
|
|
|
|
|
PremiumService.setPremiumStatus();
|
|
|
|
|
setUser(user);
|
|
|
|
|
clearMessage();
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Login successful',
|
|
|
|
|
message: `Logged in as ${user.email}`,
|
|
|
|
|
type: 'success',
|
|
|
|
|
context: 'global'
|
|
|
|
|
});
|
|
|
|
|
eSendEvent(eCloseLoginDialog);
|
2022-03-03 18:03:03 +05:00
|
|
|
await SettingsService.set({
|
2022-03-03 22:31:23 +05:00
|
|
|
sessionExpired: false,
|
|
|
|
|
userEmailConfirmed: user.isEmailConfirmed
|
2022-03-03 18:03:03 +05:00
|
|
|
});
|
2022-02-07 14:44:48 +05:00
|
|
|
eSendEvent('userLoggedIn', true);
|
|
|
|
|
await sleep(500);
|
2022-04-01 00:55:16 +05:00
|
|
|
Progress.present();
|
2022-02-07 14:44:48 +05:00
|
|
|
} catch (e) {
|
2022-03-18 14:40:15 +05:00
|
|
|
callback && callback(false);
|
2022-04-01 15:41:16 +05:00
|
|
|
console.log(
|
|
|
|
|
'Login error',
|
|
|
|
|
e.message,
|
|
|
|
|
e.data,
|
|
|
|
|
e.message === 'Multifactor authentication required.'
|
|
|
|
|
);
|
2022-03-18 14:40:15 +05:00
|
|
|
if (e.message === 'Multifactor authentication required.') {
|
2022-04-01 15:41:16 +05:00
|
|
|
console.log(TwoFactorVerification.present, 'calling 2fa');
|
|
|
|
|
setLoading(false);
|
|
|
|
|
await sleep(300);
|
2022-03-18 14:40:15 +05:00
|
|
|
TwoFactorVerification.present(async mfa => {
|
|
|
|
|
if (mfa) {
|
|
|
|
|
console.log(mfa);
|
|
|
|
|
await login(mfa);
|
|
|
|
|
} else {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}, e.data);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(e.stack);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: user ? 'Failed to sync' : 'Login failed',
|
|
|
|
|
message: e.message,
|
|
|
|
|
type: 'error',
|
|
|
|
|
context: 'local'
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-02-07 14:44:48 +05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ForgotPassword />
|
|
|
|
|
|
2022-04-01 15:41:16 +05:00
|
|
|
<SheetProvider context="two_factor_verify" />
|
2022-02-07 14:44:48 +05:00
|
|
|
{loading ? <BaseDialog transparent={true} visible={true} animation="fade" /> : null}
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
borderRadius: DDS.isTab ? 5 : 0,
|
|
|
|
|
backgroundColor: colors.bg,
|
|
|
|
|
zIndex: 10,
|
|
|
|
|
width: '100%',
|
|
|
|
|
minHeight: '100%'
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
height: 250,
|
|
|
|
|
overflow: 'hidden'
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-03-16 10:23:57 +05:00
|
|
|
<BouncingView initialScale={1.05} duration={5000}>
|
2022-02-28 13:48:59 +05:00
|
|
|
<SvgView src={SVG(colors.night ? colors.icon : 'black')} height={700} />
|
2022-02-17 00:02:41 +05:00
|
|
|
</BouncingView>
|
2022-02-07 14:44:48 +05:00
|
|
|
</View>
|
2022-04-24 05:59:14 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignSelf: 'center',
|
|
|
|
|
paddingHorizontal: 12,
|
|
|
|
|
marginBottom: 30,
|
|
|
|
|
marginTop: 15
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Heading
|
2022-02-07 14:44:48 +05:00
|
|
|
style={{
|
2022-04-24 05:59:14 +05:00
|
|
|
textAlign: 'center'
|
2022-02-07 14:44:48 +05:00
|
|
|
}}
|
2022-04-24 05:59:14 +05:00
|
|
|
size={30}
|
|
|
|
|
color={colors.heading}
|
2022-02-07 14:44:48 +05:00
|
|
|
>
|
2022-04-24 05:59:14 +05:00
|
|
|
Welcome back!
|
|
|
|
|
</Heading>
|
|
|
|
|
<Paragraph
|
2022-02-07 14:44:48 +05:00
|
|
|
style={{
|
2022-04-24 05:59:14 +05:00
|
|
|
textDecorationLine: 'underline',
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
marginTop: 5
|
|
|
|
|
}}
|
|
|
|
|
onPress={() => {
|
|
|
|
|
changeMode(1);
|
2022-02-07 14:44:48 +05:00
|
|
|
}}
|
2022-04-24 05:59:14 +05:00
|
|
|
size={SIZE.md}
|
2022-02-07 14:44:48 +05:00
|
|
|
>
|
2022-04-24 05:59:14 +05:00
|
|
|
Don't have an account? Sign up
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: focused ? '100%' : '99.9%',
|
|
|
|
|
padding: 12,
|
|
|
|
|
backgroundColor: colors.bg,
|
|
|
|
|
flexGrow: 1
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
fwdRef={emailInputRef}
|
|
|
|
|
onChangeText={value => {
|
|
|
|
|
email.current = value;
|
|
|
|
|
}}
|
|
|
|
|
testID="input.email"
|
|
|
|
|
onErrorCheck={e => setError(e)}
|
|
|
|
|
returnKeyLabel="Next"
|
|
|
|
|
returnKeyType="next"
|
|
|
|
|
autoComplete="email"
|
|
|
|
|
validationType="email"
|
|
|
|
|
autoCorrect={false}
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
errorMessage="Email is invalid"
|
|
|
|
|
placeholder="Email"
|
|
|
|
|
onSubmit={() => {
|
|
|
|
|
passwordInputRef.current?.focus();
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2022-02-17 00:02:41 +05:00
|
|
|
|
2022-04-24 05:59:14 +05:00
|
|
|
<Input
|
|
|
|
|
fwdRef={passwordInputRef}
|
|
|
|
|
onChangeText={value => {
|
|
|
|
|
password.current = value;
|
|
|
|
|
}}
|
|
|
|
|
testID="input.password"
|
|
|
|
|
returnKeyLabel="Done"
|
|
|
|
|
returnKeyType="done"
|
|
|
|
|
secureTextEntry
|
|
|
|
|
autoComplete="password"
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
autoCorrect={false}
|
|
|
|
|
placeholder="Password"
|
|
|
|
|
marginBottom={0}
|
|
|
|
|
onSubmit={() => login()}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
title="Forgot your password?"
|
|
|
|
|
style={{
|
|
|
|
|
alignSelf: 'flex-end',
|
|
|
|
|
height: 30,
|
|
|
|
|
paddingHorizontal: 0
|
|
|
|
|
}}
|
|
|
|
|
onPress={() => {
|
|
|
|
|
SheetManager.show('forgotpassword_sheet', email.current);
|
|
|
|
|
}}
|
|
|
|
|
textStyle={{
|
|
|
|
|
textDecorationLine: 'underline'
|
|
|
|
|
}}
|
|
|
|
|
fontSize={SIZE.xs}
|
|
|
|
|
type="gray"
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
// position: 'absolute',
|
|
|
|
|
marginTop: 50,
|
|
|
|
|
alignSelf: 'center'
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-02-07 14:44:48 +05:00
|
|
|
<Button
|
|
|
|
|
style={{
|
2022-04-24 05:59:14 +05:00
|
|
|
marginTop: 10,
|
|
|
|
|
width: 250,
|
|
|
|
|
borderRadius: 100
|
2022-02-07 14:44:48 +05:00
|
|
|
}}
|
2022-04-24 05:59:14 +05:00
|
|
|
loading={loading}
|
|
|
|
|
onPress={() => login()}
|
|
|
|
|
// width="100%"
|
|
|
|
|
type="accent"
|
|
|
|
|
title={loading ? null : 'Login to your account'}
|
2022-02-07 14:44:48 +05:00
|
|
|
/>
|
|
|
|
|
</View>
|
2022-04-24 05:59:14 +05:00
|
|
|
</View>
|
2022-02-07 14:44:48 +05:00
|
|
|
</View>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|