Skip to content

Commit

Permalink
feat: Expose promptForPassword function
Browse files Browse the repository at this point in the history
This allow the caller to use the password for other mean of confirmation

Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Nov 5, 2024
1 parent 30c0256 commit 5a3ca1a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
19 changes: 16 additions & 3 deletions src/components/PasswordDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export default defineComponent({
NcPasswordField,
},

props: {
callback: {

Check warning on line 63 in src/components/PasswordDialog.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Prop 'callback' requires default value to be set
type: Function,
required: false

Check warning on line 65 in src/components/PasswordDialog.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
},
},

setup() {
// non reactive props
return {
Expand Down Expand Up @@ -102,10 +109,16 @@ export default defineComponent({
return
}

const url = generateUrl('/login/confirm')
try {
const { data } = await axios.post(url, { password: this.password })
window.nc_lastLogin = data.lastLogin
if (this.callback === undefined) {
const url = generateUrl('/login/confirm')
const { data } = await axios.post(url, { password: this.password })
window.nc_lastLogin = data.lastLogin
} else {
await this.callback(this.password)
window.nc_lastLogin = Date.now() / 1000
}

this.$emit('confirmed')
} catch (e) {
this.showError = true
Expand Down
33 changes: 28 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "callback" description
* @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>) => {

Check failure on line 53 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected any. Specify a different type
return getPasswordDialog(callback)
}

/**
*
* @param mode

Check warning on line 59 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Expected @param names to be "callback". Got "mode, callback"

Check warning on line 59 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "mode" description
* @param callback

Check warning on line 60 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "callback" description
* @return

Check warning on line 61 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @return type
*/
function getPasswordDialog(callback?: (password: string) => Promise<any>): Promise<void> {

Check failure on line 63 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected any. Specify a different type
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)

Expand All @@ -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', () => {
Expand Down

0 comments on commit 5a3ca1a

Please sign in to comment.