Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose promptForPassword function #881

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires a default like

Suggested change
callback: {
callback: {
default: () => {},

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it has a default, then it won't be undefined later on and the old behavior will never be called. I think that I tried to set undefined as default, but it failed.

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 @@
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 @@
* 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 @@

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
Loading