-
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.
WIP: rewrite to withPasswordConfirmation
Signed-off-by: Louis Chemineau <[email protected]>
- Loading branch information
Showing
1 changed file
with
44 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
||
import PasswordDialogVue from './components/PasswordDialog.vue' | ||
import { DIALOG_ID, MODAL_CLASS } from './globals' | ||
|
||
import type { ComponentInstance } from 'vue' | ||
|
||
const PAGE_LOAD_TIME = Date.now() | ||
|
||
/** | ||
|
@@ -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 GitHub Actions / NPM lint
Check warning on line 49 in src/main.ts GitHub Actions / NPM lint
|
||
|
@@ -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 GitHub Actions / NPM lint
|
||
*/ | ||
export function withPasswordConfirmation(axios: Axios): Axios { | ||
const { | ||
promise: passwordDialogCallbackResolution, | ||
resolve: resolvePwdDialogCallback, | ||
reject: rejectPwdDialogCallback, | ||
} = Promise.withResolvers() | ||
|
||
axios.interceptors.request.use(async (config) => { | ||
return new Promise((resolve, reject) => { | ||
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 | ||
} |