Skip to content

Commit 36e2400

Browse files
committed
two factor authentication on fn
1 parent 708e519 commit 36e2400

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/Mutations/2faMutation.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { gql } from '@apollo/client';
2+
3+
export const EnableTwoFactorAuth = gql`
4+
mutation EnableTwoFactorAuth($email: String!) {
5+
enableTwoFactorAuth(email: $email)
6+
}
7+
`;
8+
export const VerifyOneTimeCode = gql`
9+
mutation VerifyOneTimeCode($email: String!, $code: String!) {
10+
verifyOneTimeCode(email: $email, code: $code)
11+
}
12+
`;
13+
export const DisableTwoFactorAuth = gql`
14+
mutation DisableTwoFactorAuth($email: String!) {
15+
disableTwoFactorAuth(email: $email)
16+
}
17+
`;

src/pages/Settings.tsx

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
updatePushNotifications,
1111
updateEmailNotifications,
1212
} from '../Mutations/notificationMutation';
13+
import {
14+
EnableTwoFactorAuth,
15+
DisableTwoFactorAuth,
16+
} from '../Mutations/2faMutation';
1317
import {
1418
updatedEmailNotifications,
1519
updatedPushNotifications,
@@ -24,6 +28,10 @@ function Settings() {
2428
const lan = getLanguage();
2529
const { colorTheme, setTheme } = useContext(ThemeContext);
2630
const { user } = useContext(UserContext);
31+
const [isTwoFactorEnabled, setIsTwoFactorEnabled] = useState(false);
32+
const [otpCode, setOtpCode] = useState('');
33+
const [enableTwoFactorAuth] = useMutation(EnableTwoFactorAuth);
34+
const [disableTwoFactorAuth] = useMutation(DisableTwoFactorAuth);
2735
const [updateEmailNotificationsMutation] = useMutation(
2836
updateEmailNotifications,
2937
);
@@ -42,20 +50,29 @@ function Settings() {
4250
const [emailEnabled, setEmailEnabled] = useState(
4351
data?.getUpdatedEmailNotifications || false,
4452
);
45-
53+
const handleEnableTwoFactor = async () => {
54+
try {
55+
await enableTwoFactorAuth({ variables: { email: user?.email } });
56+
setIsTwoFactorEnabled(true);
57+
} catch (error) {}
58+
};
59+
const handleDisableTwoFactor = async () => {
60+
try {
61+
await disableTwoFactorAuth({ variables: { email: user?.email } });
62+
setIsTwoFactorEnabled(false);
63+
} catch (error) {}
64+
};
4665
const handleThemeChange = (e: { target: { value: any } }) => {
4766
const { value } = e.target;
4867
setTheme(value);
4968
localStorage.setItem('color-theme', colorTheme);
5069
};
5170
const defaultTheme: any = colorTheme;
5271
const userLang = window.navigator.language;
53-
5472
const handleLanChange = (e: { target: { value: any } }) => {
5573
const { value } = e.target;
5674
i18next.changeLanguage(value);
5775
};
58-
5976
const handleEmailNotificationChange = async () => {
6077
try {
6178
const { data } = await updateEmailNotificationsMutation({
@@ -67,7 +84,6 @@ function Settings() {
6784
return `Error updating email notifications:${error}`;
6885
}
6986
};
70-
7187
const handlePushNotificationChange = async () => {
7288
try {
7389
const { data: pushData } = await updatePushNotificationsMutation({
@@ -79,25 +95,21 @@ function Settings() {
7995
return `Error updating push notifications: ${error}`;
8096
}
8197
};
82-
8398
useEffect(() => {
8499
if (data?.getUpdatedEmailNotifications !== undefined) {
85100
setEmailEnabled(data.getUpdatedEmailNotifications);
86101
}
87102
}, [data]);
88-
89103
useEffect(() => {
90104
if (pushData?.getUpdatedPushNotifications !== undefined) {
91105
setPushEnabled(pushData.getUpdatedPushNotifications);
92106
}
93107
}, [pushData]);
94-
95108
useEffect(() => {
96109
if (lanRef.current) {
97110
lanRef.current.value = lan;
98111
}
99112
}, []);
100-
101113
return (
102114
<div className="flex flex-col grow bg-light-bg dark:bg-dark-frame-bg">
103115
<div className="flex flex-row justify-center pt-10">
@@ -220,15 +232,28 @@ function Settings() {
220232
{t('Privacy and Security')}
221233
</h1>
222234
<p className="text-sm text-gray-600 dark:text-dark-text-fill">
223-
{t('Privacy and Security')}
235+
{t('Secure your account with two-factor authentication')}
224236
</p>
225237
</div>
226-
<Link
227-
className="ml-auto mt-2 text-xs md:text-base text-gray-600 dark:text-dark-text-fill"
228-
to="#link"
238+
<Switch
239+
checked={isTwoFactorEnabled}
240+
onChange={
241+
isTwoFactorEnabled
242+
? handleDisableTwoFactor
243+
: handleEnableTwoFactor
244+
}
245+
className={`ml-auto border ${
246+
isTwoFactorEnabled ? 'dark:border-primary' : ''
247+
} relative inline-flex h-6 w-12 items-center rounded-full`}
229248
>
230-
<h4>{t('Change')}</h4>
231-
</Link>
249+
<span
250+
className={`${
251+
isTwoFactorEnabled
252+
? 'bg-primary translate-x-6'
253+
: 'bg-gray-300 translate-x-1'
254+
} inline-block h-4 w-4 transform rounded-full`}
255+
/>
256+
</Switch>
232257
</li>
233258
<li className="flex items-center pt-2 pb-1">
234259
<div className="w-[33vw]">
@@ -252,5 +277,4 @@ function Settings() {
252277
</div>
253278
);
254279
}
255-
256280
export default Settings;

0 commit comments

Comments
 (0)