Skip to content

Commit c8f814a

Browse files
committed
feat: update TwoFaModal and plugin to handle custom brand prefix for issuer name
1 parent c5925a5 commit c8f814a

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

custom/TwoFaModal.vue

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
<script setup lang="ts">
3636
import VOtpInput from 'vue3-otp-input';
37-
import { ref, nextTick, onMounted, onBeforeUnmount } from 'vue';
37+
import { ref, nextTick, watch } from 'vue';
3838
import { useUserStore } from '@/stores/user';
3939
import { useI18n } from 'vue-i18n';
4040
declare global {
@@ -131,15 +131,19 @@
131131
emit('rejected', new Error('cancelled'));
132132
emit('closed');
133133
}
134-
135-
onMounted(async () => {
134+
135+
watch(modelShow, async (open) => {
136+
if (open) {
136137
await nextTick();
137138
tagOtpInputs();
138-
window.addEventListener('paste', handlePaste as any);
139-
});
140-
onBeforeUnmount(() => {
141-
window.removeEventListener('paste', handlePaste as any);
142-
});
139+
console.log('adding paste listener');
140+
window.addEventListener('paste', handlePaste);
141+
} else {
142+
console.log('re paste listener');
143+
window.removeEventListener('paste', handlePaste);
144+
}
145+
});
146+
143147
</script>
144148

145149
<style scoped>

index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
9393
const userName = adminUser.dbUser[adminforth.config.auth.usernameField]
9494
const brandName = adminforth.config.customization.brandName;
9595
const brandNameSlug = adminforth.config.customization.brandNameSlug;
96+
const issuerName = (this.options.customBrendPrefix && this.options.customBrendPrefix.trim())
97+
? this.options.customBrendPrefix.trim()
98+
: brandName;
9699
const authResource = adminforth.config.resources.find((res)=>res.resourceId === adminforth.config.auth.usersResourceId )
97100
const authPk = authResource.columns.find((col)=>col.primaryKey).name
98101
const userPk = adminUser.dbUser[authPk]
@@ -108,10 +111,10 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
108111
const userCanSkipSetup = this.options.usersFilterToAllowSkipSetup ? this.options.usersFilterToAllowSkipSetup(adminUser) : false;
109112

110113
if (!secret){
111-
const tempSecret = twofactor.generateSecret({name: brandName,account: userName})
114+
const tempSecret = twofactor.generateSecret({name: issuerName,account: userName})
112115
newSecret = tempSecret.secret
113116
} else {
114-
const value = this.adminforth.auth.issueJWT({userName, issuer:brandName, pk:userPk, userCanSkipSetup, rememberMeDays }, 'tempTotp', '2h');
117+
const value = this.adminforth.auth.issueJWT({userName, issuer:issuerName, pk:userPk, userCanSkipSetup, rememberMeDays }, 'tempTotp', '2h');
115118
response.setHeader('Set-Cookie', `adminforth_${brandNameSlug}_totpTemporaryJWT=${value}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; max-age=3600; `);
116119

117120
return {
@@ -122,7 +125,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
122125
ok: true
123126
}
124127
}
125-
const totpTemporaryJWT = this.adminforth.auth.issueJWT({userName, newSecret, issuer:brandName, pk:userPk, userCanSkipSetup, rememberMeDays }, 'tempTotp', '2h');
128+
const totpTemporaryJWT = this.adminforth.auth.issueJWT({userName, newSecret, issuer:issuerName, pk:userPk, userCanSkipSetup, rememberMeDays }, 'tempTotp', '2h');
126129
response.setHeader('Set-Cookie', `adminforth_${brandNameSlug}_totpTemporaryJWT=${totpTemporaryJWT}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; Expires=${new Date(Date.now() + '1h').toUTCString() } `);
127130

128131
return {

types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export type PluginOptions = {
1616
*/
1717
timeStepWindow?: number;
1818

19+
customBrendPrefix?: string;
20+
1921
/**
2022
* Optional function to filter users to apply 2FA.
2123
* Should return true if 2FA should be applied to the user and false if AdminForth should not challenge the user with 2FA.

0 commit comments

Comments
 (0)