Files
notesnook/apps/mobile/src/components/auth/login.js

260 lines
7.5 KiB
JavaScript
Raw Normal View History

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 { useThemeStore } from '../../stores/theme';
import { useUserStore } from '../../stores/stores';
import { DDS } from '../../services/device-detection';
import { eSendEvent, presentSheet, ToastEvent } from '../../services/event-manager';
import { clearMessage } from '../../services/message';
import PremiumService from '../../services/premium';
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 { MMKV } from '../../utils/database/mmkv';
import { SIZE } from '../../utils/size';
import { sleep } from '../../utils/time';
import { IconButton } from '../ui/icon-button';
import { Button } from '../ui/button';
import BaseDialog from '../dialog/base-dialog';
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-03 18:03:03 +05:00
import SettingsService from '../../services/settings';
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);
};
}, []);
const login = async () => {
if (!validateInfo() || error) return;
setLoading(true);
let user;
try {
2022-02-17 00:02:41 +05:00
await db.user.login(email.current.toLowerCase(), password.current);
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({
sessionExpired: false
});
2022-02-07 14:44:48 +05:00
eSendEvent('userLoggedIn', true);
await sleep(500);
presentSheet({
title: 'Syncing your data',
paragraph: 'Please wait while we sync all your data.',
progress: true
});
} catch (e) {
2022-02-17 00:02:41 +05:00
console.log(e.stack);
2022-02-07 14:44:48 +05:00
setLoading(false);
ToastEvent.show({
heading: user ? 'Failed to sync' : 'Login failed',
message: e.message,
type: 'error',
context: 'local'
});
}
};
return (
<>
2022-02-28 13:48:59 +05:00
<IconButton
2022-02-07 14:44:48 +05:00
name="arrow-left"
onPress={() => {
eSendEvent(eCloseLoginDialog);
}}
color={colors.pri}
customStyle={{
position: 'absolute',
zIndex: 999,
left: 12,
2022-02-17 00:02:41 +05:00
top: Platform.OS === 'ios' ? 12 + insets.top : 12
2022-02-07 14:44:48 +05:00
}}
/>
<ForgotPassword />
{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-02-17 00:02:41 +05:00
<BouncingView initialScale={1.2} 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-02-17 00:02:41 +05:00
<BouncingView initialScale={0.95} duration={3000}>
<View
2022-02-07 14:44:48 +05:00
style={{
2022-02-17 00:02:41 +05:00
width: '100%',
justifyContent: 'center',
alignSelf: 'center',
paddingHorizontal: 12,
marginBottom: 30,
marginTop: 15
2022-02-07 14:44:48 +05:00
}}
>
2022-02-17 00:02:41 +05:00
<Heading
style={{
textAlign: 'center'
}}
size={30}
color={colors.heading}
>
Welcome back!
</Heading>
<Paragraph
style={{
textDecorationLine: 'underline',
textAlign: 'center',
marginTop: 5
}}
onPress={() => {
changeMode(1);
}}
size={SIZE.md}
>
Don't have an account? Sign up
</Paragraph>
</View>
2022-02-07 14:44:48 +05:00
<View
style={{
2022-02-17 00:02:41 +05:00
width: focused ? '100%' : '99.9%',
padding: 12,
backgroundColor: colors.bg,
flexGrow: 1
2022-02-07 14:44:48 +05:00
}}
>
2022-02-17 00:02:41 +05:00
<Input
fwdRef={emailInputRef}
onChangeText={value => {
email.current = value;
}}
onErrorCheck={e => setError(e)}
returnKeyLabel="Next"
returnKeyType="next"
2022-02-28 13:48:59 +05:00
autoComplete="email"
2022-02-17 00:02:41 +05:00
validationType="email"
autoCorrect={false}
autoCapitalize="none"
errorMessage="Email is invalid"
placeholder="Email"
onSubmit={() => {}}
/>
<Input
fwdRef={passwordInputRef}
onChangeText={value => {
password.current = value;
}}
onErrorCheck={e => setError(e)}
returnKeyLabel="Done"
returnKeyType="done"
secureTextEntry
2022-02-28 13:48:59 +05:00
autoComplete="password"
2022-02-17 00:02:41 +05:00
autoCapitalize="none"
validationType="password"
autoCorrect={false}
placeholder="Password"
marginBottom={0}
/>
2022-02-07 14:44:48 +05:00
<Button
2022-02-17 00:02:41 +05:00
title="Forgot your password?"
2022-02-07 14:44:48 +05:00
style={{
2022-02-17 00:02:41 +05:00
alignSelf: 'flex-end',
height: 30,
paddingHorizontal: 0
}}
onPress={() => {
SheetManager.show('forgotpassword_sheet', email.current);
2022-02-07 14:44:48 +05:00
}}
2022-02-17 00:02:41 +05:00
textStyle={{
textDecorationLine: 'underline'
}}
fontSize={SIZE.xs}
type="gray"
2022-02-07 14:44:48 +05:00
/>
2022-02-17 00:02:41 +05:00
<View
style={{
// position: 'absolute',
marginTop: 50,
alignSelf: 'center'
}}
>
<Button
style={{
marginTop: 10,
width: 250,
borderRadius: 100
}}
loading={loading}
onPress={login}
// width="100%"
type="accent"
title={loading ? null : 'Login to your account'}
/>
</View>
2022-02-07 14:44:48 +05:00
</View>
2022-02-17 00:02:41 +05:00
</BouncingView>
2022-02-07 14:44:48 +05:00
</View>
</>
);
};