Skip to content

Commit

Permalink
WIP: rewrite to withPasswordConfirmation
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Nov 6, 2024
1 parent 5a3ca1a commit b9696a6
Showing 1 changed file with 44 additions and 14 deletions.
58 changes: 44 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
* SPDX-License-Identifier: MIT
*/
import Vue from 'vue'
import type { ComponentInstance } from 'vue'

import { Axios } from '@nextcloud/axios'
import { getCurrentUser } from '@nextcloud/auth'

Check failure on line 9 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

"@nextcloud/auth" is extraneous

import PasswordDialogVue from './components/PasswordDialog.vue'
import { DIALOG_ID, MODAL_CLASS } from './globals'

import type { ComponentInstance } from 'vue'

const PAGE_LOAD_TIME = Date.now()

/**
Expand Down Expand Up @@ -42,18 +44,6 @@ export const confirmPassword = (): Promise<void> => {
return getPasswordDialog()
}

/**
* Ask the password to the user for later use.
*
* @param callback
* @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 49 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 49 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "mode" description
Expand Down Expand Up @@ -97,3 +87,43 @@ function getPasswordDialog(callback?: (password: string) => Promise<any>): Promi
})
})
}

/**
* Add interceptors to an axios instance that for every request
* will prompt for password confirmation and add it as Basic Auth.
* @param axios

Check warning on line 94 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "axios" description
*/
export function withPasswordConfirmation(axios: Axios): Axios {
const {
promise: passwordDialogCallbackResolution,
resolve: resolvePwdDialogCallback,
reject: rejectPwdDialogCallback,

Check failure on line 100 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

'rejectPwdDialogCallback' is assigned a value but never used
} = Promise.withResolvers()

axios.interceptors.request.use(async (config) => {
return new Promise((resolve, reject) => {

Check failure on line 104 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

'reject' is defined but never used
getPasswordDialog((password: string) => {
resolve({
...config,
auth: {
username: getCurrentUser()?.uid ?? '',
password,
},
})

// Await for request to be done
return passwordDialogCallbackResolution
})
})
})

axios.interceptors.response.use((response) => {
if (response.request.auth !== undefined) {
resolvePwdDialogCallback(undefined)
}

return response
})

return axios
}

0 comments on commit b9696a6

Please sign in to comment.