Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: 2fa with sms & email

ammarahm-ed2a8aa2af431e
diff --git a/apps/mobile/src/components/auth/login.js b/apps/mobile/src/components/auth/login.js
index 6d97c70c8..0a9fa4289 100644
--- a/apps/mobile/src/components/auth/login.js
+++ b/apps/mobile/src/components/auth/login.js
@@ -2,20 +2,21 @@ 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 { 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';
+import SettingsService from '../../services/settings';
+import { useUserStore } from '../../stores/stores';
+import { useThemeStore } from '../../stores/theme';
import { db } from '../../utils/database';
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 SheetProvider from '../sheet-provider';
+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';
@@ -23,8 +24,6 @@ import Heading from '../ui/typography/heading';
import Paragraph from '../ui/typography/paragraph';
import { SVG } from './background';
import { ForgotPassword } from './forgot-password';
-import SettingsService from '../../services/settings';
-import SheetProvider from '../sheet-provider';
import TwoFactorVerification from './two-factor';
export const Login = ({ changeMode }) => {
diff --git a/apps/mobile/src/components/auth/twofactor.js b/apps/mobile/src/components/auth/twofactor.js
index 153aa91e9..41c242f73 100644
--- a/apps/mobile/src/components/auth/twofactor.js
+++ b/apps/mobile/src/components/auth/twofactor.js
@@ -1,4 +1,4 @@
-import React, { useRef, useState } from 'react';
+import React, { useEffect, useRef, useState } from 'react';
import { View } from 'react-native';
import { eSendEvent, presentSheet } from '../../services/event-manager';
import { useThemeStore } from '../../stores/theme';
@@ -12,6 +12,8 @@ import { PressableButton } from '../ui/pressable';
import Seperator from '../ui/seperator';
import Heading from '../ui/typography/heading';
import Paragraph from '../ui/typography/paragraph';
+import { db } from '../../utils/database/index';
+import { ToastEvent } from '../../services/event-manager';
const TwoFactorVerification = ({ onMfaLogin, mfaInfo }) => {
const colors = useThemeStore(state => state.colors);
@@ -23,6 +25,7 @@ const TwoFactorVerification = ({ onMfaLogin, mfaInfo }) => {
const { seconds, start } = useTimer(currentMethod.method);
const [loading, setLoading] = useState(false);
const inputRef = useRef();
+ const [sending, setSending] = useState(false);
const codeHelpText = {
app: 'Enter the 6 digit code from your authenticator app to continue logging in',
@@ -100,6 +103,27 @@ const TwoFactorVerification = ({ onMfaLogin, mfaInfo }) => {
);
};
+ useEffect(() => {
+ if (currentMethod.method === 'sms' || currentMethod.method === 'email') {
+ onSendCode();
+ }
+ }, [currentMethod.method]);
+
+ const onSendCode = async () => {
+ if (seconds || sending) return;
+ // TODO
+ setSending(true);
+ try {
+ console.log('sending code');
+ await db.mfa.sendCode(currentMethod.method, mfaInfo.token);
+ start(60);
+ setSending(false);
+ } catch (e) {
+ setSending(false);
+ ToastEvent.error(e, 'Error sending 2FA Code', 'local');
+ }
+ };
+
return (
<View>
<View
@@ -137,14 +161,12 @@ const TwoFactorVerification = ({ onMfaLogin, mfaInfo }) => {
<Seperator />
- {currentMethod.method === 'sms' || currentMethod === 'email' ? (
+ {currentMethod.method === 'sms' || currentMethod.method === 'email' ? (
<Button
- onPress={() => {
- if (seconds) return;
- start(60);
- }}
+ onPress={onSendCode}
type={seconds ? 'gray' : 'transparent'}
- title={`${seconds ? `Resend code in (${seconds})` : 'Send code'}`}
+ title={sending ? '' : `${seconds ? `Resend code in (${seconds})` : 'Send code'}`}
+ loading={sending}
height={30}
/>
) : null}
diff --git a/apps/mobile/src/screens/settings/2fa.tsx b/apps/mobile/src/screens/settings/2fa.tsx
index 48cb1afdd..a0c1f895b 100644
--- a/apps/mobile/src/screens/settings/2fa.tsx
+++ b/apps/mobile/src/screens/settings/2fa.tsx
@@ -46,11 +46,13 @@ const TwoFactorAuth = ({ isSheet }: { isSheet: boolean }) => {
const mfaSettingsList = user?.mfa.isEnabled
? [
{
- name: 'Add secondary 2FA methods',
+ name: user.mfa.secondaryMethod
+ ? 'Reconfigure fallback 2FA method'
+ : 'Add fallback 2FA method',
func: () => {
MFASheet.present(true);
},
- desc: 'Add alternate 2FA methods to protect your account'
+ desc: 'You can use fallback 2FA method incase you are unable to login via primary method'
},
{
name: 'View recovery codes',
@@ -64,7 +66,7 @@ const TwoFactorAuth = ({ isSheet }: { isSheet: boolean }) => {
{
name: 'Disable Two Factor Authentication',
func: disable2fa,
- desc: 'Increased security for your account'
+ desc: 'Decreased security for your account'
}
]
: [
@@ -216,6 +218,7 @@ const MFASetup = ({ method, onSuccess, setStep, recovery }: MFAStepProps) => {
const [loading, setLoading] = useState(method?.id === 'app' ? true : false);
const [enabling, setEnabling] = useState(false);
+ const [sending, setSending] = useState(false);
useEffect(() => {
if (method?.id === 'app') {
@@ -237,7 +240,14 @@ const MFASetup = ({ method, onSuccess, setStep, recovery }: MFAStepProps) => {
try {
if (!method) return;
setEnabling(true);
- await db.mfa?.enable(method.id, code.current);
+ if (recovery) {
+ console.log('enabling secondary method for 2FA');
+ await db.mfa?.enableFallback(method.id, code.current);
+ } else {
+ console.log('enabling primary method for 2FA');
+ await db.mfa?.enable(method.id, code.current);
+ }
+
let user = await db.user?.fetchUser();
useUserStore.getState().setUser(user);
onSuccess && onSuccess(method);
@@ -252,7 +262,7 @@ const MFASetup = ({ method, onSuccess, setStep, recovery }: MFAStepProps) => {
const onSendCode = async () => {
console.log('send code');
- if (!method) return;
+ if (!method || sending) return;
if (method.id === 'app' && authenticatorDetails.sharedKey) {
Clipboard.setString(authenticatorDetails.sharedKey);
ToastEvent.show({
@@ -266,8 +276,8 @@ const MFASetup = ({ method, onSuccess, setStep, recovery }: MFAStepProps) => {
try {
console.log('seconds', seconds);
if (seconds) throw new Error('Please wait a few seconds before resending code');
- if (method.id === 'sms' && !phoneNumber.current) return;
-
+ if (method.id === 'sms' && !phoneNumber.current) throw new Error('Phone number not entered');
+ setSending(true);
await db.mfa?.setup(method?.id, phoneNumber.current);
if (method.id === 'sms') {
@@ -275,13 +285,15 @@ const MFASetup = ({ method, onSuccess, setStep, recovery }: MFAStepProps) => {
setId(method.id + phoneNumber.current);
}
await sleep(300);
- start(60, method.id + phoneNumber.current);
+ start(60, method.id === 'sms' ? method.id + phoneNumber.current : method.id);
+ setSending(false);
ToastEvent.show({
heading: `2FA code sent via ${method.id}.`,
type: 'success',
context: 'local'
});
} catch (e) {
+ setSending(false);
const error = e as Error;
console.log('Send code:', error.message);
ToastEvent.error(error, 'Error sending 2FA code');
@@ -335,8 +347,11 @@ const MFASetup = ({ method, onSuccess, setStep, recovery }: MFAStepProps) => {
buttons={
<Button
onPress={onSendCode}
+ loading={sending}
title={
- method.id === 'app'
+ sending
+ ? null
+ : method.id === 'app'
? 'Copy'
: `${seconds ? `Resend code in (${seconds})` : 'Send code'}`
}
@@ -599,14 +614,16 @@ const MFASuccess = ({ recovery }: MFAStepProps) => {
}}
/>
- <Button
- title="Setup secondary 2FA method"
- type="gray"
- height={25}
- onPress={() => {
- MFASheet.present(true);
- }}
- />
+ {!recovery ? (
+ <Button
+ title="Setup secondary 2FA method"
+ type="gray"
+ height={25}
+ onPress={() => {
+ MFASheet.present(true);
+ }}
+ />
+ ) : null}
</View>
);
};