Skip to content

Commit 97c835a

Browse files
authored
Merge pull request #547 from devforth/update-docs-fot-the-2fa
docs: update docs for the 2fa plugin
2 parents 5e5aa93 + eed415a commit 97c835a

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

adminforth/documentation/docs/tutorial/09-Plugins/02-TwoFactorsAuth.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ But if you websocket doesn't work in you application, or you wan't to perform ve
283283
284284
You might want to to allow to call some custom critical/money related actions with additional 2FA approval. This eliminates risks caused by user cookies theft by some virous/doorway software after login.
285285
286-
To do it, first, create frontend custom component which wraps and intercepts click event to menu item, and in click handler do a call to `window.adminforthTwoFaModal.getCode(cb?)` frontend API exposed by this plugin. This is awaitable call wich shows 2FA popup and asks user to authenticate with 2nd factor (if passkey is enabled it will be suggested first, with ability to fallback to TOTP)
286+
To do it, first, create frontend custom component which wraps and intercepts click event to menu item, and in click handler do a call to `get2FaConfirmationResult` frontend API exposed by this plugin. This is awaitable call wich shows 2FA popup and asks user to authenticate with 2nd factor (if passkey is enabled it will be suggested first, with ability to fallback to TOTP)
287287
288288
```ts title='/custom/RequireTwoFaGate.vue'
289289
<template>
@@ -293,6 +293,9 @@ To do it, first, create frontend custom component which wraps and intercepts cli
293293
</template>
294294

295295
<script setup lang="ts">
296+
import { useTwoFactorsAuth } from '@/custom/plugins/TwoFactorsAuthPlugin/use2faApi.ts';
297+
298+
const { get2FaConfirmationResult } = useTwoFactorsAuth();
296299
const emit = defineEmits<{ (e: 'callAction', payload?: any): void }>();
297300
const props = defineProps<{ disabled?: boolean; meta?: Record<string, any> }>();
298301

@@ -301,8 +304,7 @@ To do it, first, create frontend custom component which wraps and intercepts cli
301304
return;
302305
}
303306

304-
const verificationResult = await window.adminforthTwoFaModal.get2FaConfirmationResult(); // this will ask user to enter code
305-
307+
const verificationResult = await get2FaConfirmationResult(); // this will ask user to enter code
306308
emit('callAction', { verificationResult }); // then we pass this verification result to action (from fronted to backend)
307309
}
308310
</script>
@@ -387,20 +389,19 @@ Frontend (Save Interceptor component injected via pageInjections):
387389
```vue title='/custom/SaveInterceptor.vue'
388390
<script setup>
389391
import { useAdminforth } from '@/adminforth';
392+
import { useTwoFactorsAuth } from '@/custom/plugins/TwoFactorsAuthPlugin/use2faApi.ts';
390393

394+
const { get2FaConfirmationResult } = useTwoFactorsAuth();
391395
const { registerSaveInterceptor } = useAdminforth();
392396

393397
registerSaveInterceptor(async ({ action, values, resource }) => {
394398
// action is 'create' or 'edit'
395-
const modal = (window as any)?.adminforthTwoFaModal;
396-
if (modal?.get2FaConfirmationResult) {
397-
const confirmationResult = await modal.get2FaConfirmationResult('Confirm to save changes');
398-
if (!confirmationResult) {
399-
return { ok: false, error: 'Two-factor authentication cancelled' };
400-
}
401-
// Pass data to backend; the view will forward extra.confirmationResult to meta.confirmationResult
402-
return { ok: true, extra: { confirmationResult } };
399+
const confirmationResult = await get2FaConfirmationResult('Confirm to save changes');
400+
if (!confirmationResult) {
401+
return { ok: false, error: 'Two-factor authentication cancelled' };
403402
}
403+
// Pass data to backend; the view will forward extra.confirmationResult to meta.confirmationResult
404+
return { ok: true, extra: { confirmationResult } };
404405
else {
405406
throw new Error('No Two-Factor Authentication modal found, please ensure you have latest version of @adminforth/two-factors-auth installed and instantiated on resource');
406407
}
@@ -493,9 +494,12 @@ Imagine you have some button which does some API call
493494

494495
<script setup lang="ts">
495496
import { callApi } from '@/utils';
497+
import { useTwoFactorsAuth } from '@/custom/plugins/TwoFactorsAuthPlugin/use2faApi.ts';
498+
499+
const { get2FaConfirmationResult } = useTwoFactorsAuth();
496500

497501
async function callAdminAPI() {
498-
const verificationResult = await window.adminforthTwoFaModal.get2FaConfirmationResult();
502+
const verificationResult = await get2FaConfirmationResult();
499503

500504
const res = await callApi({
501505
path: '/myCriticalAction',
@@ -534,12 +538,14 @@ You might want to protect this call with a second factor also. To do it, we need
534538
<script setup lang="ts">
535539
import { callApi } from '@/utils';
536540
import { useAdminforth } from '@/adminforth';
541+
import { useTwoFactorsAuth } from '@/custom/plugins/TwoFactorsAuthPlugin/use2faApi.ts';
537542

543+
const { get2FaConfirmationResult } = useTwoFactorsAuth();
538544
const { alert } = useAdminforth();
539545

540546
async function callAdminAPI() {
541547
// diff-add
542-
const verificationResult = await window.adminforthTwoFaModal.get2FaConfirmationResult();
548+
const verificationResult = await get2FaConfirmationResult();
543549

544550
const res = await callApi({
545551
path: '/myCriticalAction',

dev-demo/custom/SystemTest.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ const { alert } = useAdminforth();
4141
import adminforth from '@/adminforth';
4242
import { useTwoFactorsAuth } from '@/custom/plugins/TwoFactorsAuthPlugin/use2faApi.ts';
4343
44-
const twoFactorsAuth = useTwoFactorsAuth();
45-
44+
const { get2FaConfirmationResult } = useTwoFactorsAuth();
4645
const valueStart = ref()
4746
4847
watch(valueStart, (newVal) => {
@@ -105,7 +104,7 @@ async function get2FaConfirmationResultWindow() {
105104
}
106105
107106
async function get2FaConfirmationResultTwoFactorsAuth() {
108-
const verificationResult = await twoFactorsAuth.get2FaConfirmationResult(); // this will ask user to enter code
107+
const verificationResult = await get2FaConfirmationResult(); // this will ask user to enter code
109108
console.log('2FA verification result (twoFactorsAuth):', verificationResult);
110109
}
111110
</script>

0 commit comments

Comments
 (0)