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

286 lines
8.0 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useRef, useState } from "react";
import { Platform, View } from "react-native";
import { SheetManager } from "react-native-actions-sheet";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { DDS } from "../../services/device-detection";
import { eSendEvent, ToastEvent } from "../../services/event-manager";
import { clearMessage } from "../../services/message";
import PremiumService from "../../services/premium";
import SettingsService from "../../services/settings";
import { useUserStore } from "../../stores/use-user-store";
import { useThemeStore } from "../../stores/use-theme-store";
import { db } from "../../common/database";
import { eCloseLoginDialog } from "../../utils/events";
import { SIZE } from "../../utils/size";
import { sleep } from "../../utils/time";
import BaseDialog from "../dialog/base-dialog";
import SheetProvider from "../sheet-provider";
import { Progress } from "../sheets/progress";
import { Button } from "../ui/button";
import { IconButton } from "../ui/icon-button";
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";
import { SVG } from "./background";
import { ForgotPassword } from "./forgot-password";
import TwoFactorVerification from "./two-factor";
import Animated, {
FadeInDown,
FadeOutDown,
FadeOutUp
} from "react-native-reanimated";
import Navigation from "../../services/navigation";
import { hideAuth } from "./common";
2022-07-05 14:33:48 +05:00
export const Login = ({ changeMode, welcome }) => {
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);
2022-02-07 14:44:48 +05:00
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"
2022-02-07 14:44:48 +05:00
});
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;
try {
2022-03-18 14:40:15 +05:00
if (mfa) {
await db.user.mfaLogin(
email.current.toLowerCase(),
password.current,
mfa
);
2022-03-18 14:40:15 +05:00
} else {
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!");
2022-02-07 14:44:48 +05:00
PremiumService.setPremiumStatus();
setUser(user);
clearMessage();
ToastEvent.show({
heading: "Login successful",
2022-02-07 14:44:48 +05:00
message: `Logged in as ${user.email}`,
type: "success",
context: "global"
2022-02-07 14:44:48 +05:00
});
2022-07-05 14:33:48 +05:00
hideAuth();
SettingsService.set({
2022-03-03 22:31:23 +05:00
sessionExpired: false,
2022-07-09 09:45:16 +05:00
userEmailConfirmed: user?.isEmailConfirmed
2022-03-03 18:03:03 +05:00
});
eSendEvent("userLoggedIn", true);
2022-02-07 14:44:48 +05:00
await sleep(500);
Progress.present();
2022-02-07 14:44:48 +05:00
} catch (e) {
2022-03-18 14:40:15 +05:00
callback && callback(false);
if (e.message === "Multifactor authentication required.") {
setLoading(false);
await sleep(300);
TwoFactorVerification.present(async (mfa) => {
2022-03-18 14:40:15 +05:00
if (mfa) {
console.log(mfa);
await login(mfa);
} else {
setLoading(false);
}
}, e.data);
} else {
setLoading(false);
ToastEvent.show({
heading: user ? "Failed to sync" : "Login failed",
2022-03-18 14:40:15 +05:00
message: e.message,
type: "error",
context: "local"
2022-03-18 14:40:15 +05:00
});
}
2022-02-07 14:44:48 +05:00
}
};
return (
<>
<ForgotPassword />
<SheetProvider context="two_factor_verify" />
{loading ? (
<BaseDialog transparent={true} visible={true} animation="fade" />
) : null}
2022-07-05 14:33:48 +05:00
<Animated.View
entering={FadeInDown}
exiting={FadeOutUp}
2022-02-07 14:44:48 +05:00
style={{
borderRadius: DDS.isTab ? 5 : 0,
backgroundColor: colors.bg,
zIndex: 10,
width: "100%",
minHeight: "100%"
2022-02-07 14:44:48 +05:00
}}
>
<View
style={{
height: 250,
overflow: "hidden"
2022-02-07 14:44:48 +05:00
}}
>
<SvgView
src={SVG(colors.night ? colors.icon : "black")}
height={700}
/>
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",
2022-04-24 05:59:14 +05:00
paddingHorizontal: 12,
marginBottom: 30,
marginTop: 15
}}
>
<Heading
2022-02-07 14:44:48 +05:00
style={{
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={{
textDecorationLine: "underline",
textAlign: "center",
2022-04-24 05:59:14 +05:00
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: DDS.isTab
? focused
? "50%"
: "49.99%"
: focused
? "100%"
: "99.9%",
2022-04-24 05:59:14 +05:00
padding: 12,
backgroundColor: colors.bg,
2022-07-11 13:00:22 +05:00
flexGrow: 1,
alignSelf: "center"
2022-04-24 05:59:14 +05:00
}}
>
<Input
fwdRef={emailInputRef}
onChangeText={(value) => {
2022-04-24 05:59:14 +05:00
email.current = value;
}}
testID="input.email"
onErrorCheck={(e) => setError(e)}
2022-04-24 05:59:14 +05:00
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) => {
2022-04-24 05:59:14 +05:00
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",
2022-04-24 05:59:14 +05:00
height: 30,
paddingHorizontal: 0
}}
onPress={() => {
SheetManager.show("forgotpassword_sheet", email.current);
2022-04-24 05:59:14 +05:00
}}
textStyle={{
textDecorationLine: "underline"
2022-04-24 05:59:14 +05:00
}}
fontSize={SIZE.xs}
type="gray"
/>
<View
style={{
// position: 'absolute',
2022-07-05 14:33:48 +05:00
marginTop: 25,
alignSelf: "center"
2022-04-24 05:59:14 +05:00
}}
>
2022-02-07 14:44:48 +05:00
<Button
style={{
2022-04-24 05:59:14 +05:00
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-07-05 14:33:48 +05:00
</Animated.View>
2022-02-07 14:44:48 +05:00
</>
);
};