-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose promptForPassword function
This allow the caller to use the password for other mean of confirmation Signed-off-by: Louis Chemineau <louis@chmn.me>
- Loading branch information
Showing
2 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* SPDX-License-Identifier: MIT | ||
*/ | ||
import Vue from 'vue' | ||
|
||
import PasswordDialogVue from './components/PasswordDialog.vue' | ||
import { DIALOG_ID, MODAL_CLASS } from './globals' | ||
|
||
|
@@ -34,15 +35,37 @@ export const isPasswordConfirmationRequired = (): boolean => { | |
* or confirmation is already in process. | ||
*/ | ||
export const confirmPassword = (): Promise<void> => { | ||
if (!isPasswordConfirmationRequired()) { | ||
return Promise.resolve() | ||
} | ||
|
||
return getPasswordDialog() | ||
} | ||
|
||
/** | ||
* Ask the password to the user for later use. | ||
* | ||
* @param callback | ||
Check warning on line 48 in src/main.ts GitHub Actions / NPM lint
|
||
* @return {Promise<void>} Promise that resolves when password is submitted. | ||
* Rejects if password confirmation was cancelled | ||
* or confirmation is already in process. | ||
*/ | ||
export const promptForPassword = (callback: (password: string) => Promise<any>) => { | ||
return getPasswordDialog(callback) | ||
} | ||
|
||
/** | ||
* | ||
* @param mode | ||
Check warning on line 59 in src/main.ts GitHub Actions / NPM lint
Check warning on line 59 in src/main.ts GitHub Actions / NPM lint
|
||
* @param callback | ||
Check warning on line 60 in src/main.ts GitHub Actions / NPM lint
|
||
* @return | ||
*/ | ||
function getPasswordDialog(callback?: (password: string) => Promise<any>): Promise<void> { | ||
const isDialogMounted = Boolean(document.getElementById(DIALOG_ID)) | ||
if (isDialogMounted) { | ||
return Promise.reject(new Error('Password confirmation dialog already mounted')) | ||
} | ||
|
||
if (!isPasswordConfirmationRequired()) { | ||
return Promise.resolve() | ||
} | ||
|
||
const mountPoint = document.createElement('div') | ||
mountPoint.setAttribute('id', DIALOG_ID) | ||
|
||
|
@@ -61,7 +84,7 @@ export const confirmPassword = (): Promise<void> => { | |
|
||
const DialogClass = Vue.extend(PasswordDialogVue) | ||
// Mount point element is replaced by the component | ||
const dialog = (new DialogClass() as ComponentInstance).$mount(mountPoint) | ||
const dialog = (new DialogClass({ propsData: { callback } }) as ComponentInstance).$mount(mountPoint) | ||
|
||
return new Promise((resolve, reject) => { | ||
dialog.$on('confirmed', () => { | ||
|