Skip to content

Commit

Permalink
WIP: Add reminder mode for pwd confirmation
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Nov 7, 2024
1 parent b9696a6 commit 5579613
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
14 changes: 3 additions & 11 deletions src/components/PasswordDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<NcButton class="vue-password-confirmation__submit"
type="primary"
native-type="submit"
:disabled="!password">
:disabled="!password || loading">
<template v-if="loading" #icon>
<NcLoadingIcon :size="20" />
</template>
Expand Down Expand Up @@ -62,7 +62,7 @@ export default defineComponent({
props: {
callback: {
type: Function,
required: false
required: true,
},
},
Expand Down Expand Up @@ -110,15 +110,7 @@ export default defineComponent({
}
try {
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
}
await this.callback(this.password)
this.$emit('confirmed')
} catch (e) {
this.showError = true
Expand Down
57 changes: 39 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,57 @@ function getPasswordDialog(callback?: (password: string) => Promise<any>): Promi
* @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,
} = Promise.withResolvers()
let resolvePwdDialogCallback: (value: unknown) => void
let rejectPwdDialogCallback: (reason?: any) => void

Check failure on line 98 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected any. Specify a different type
let firstTry = true

axios.interceptors.request.use(async (config) => {
const pwdConfirmationAxios = axios.create() as Axios

// TODO: copy axios instance
pwdConfirmationAxios.interceptors.request.use(async (config) => {
return new Promise((resolve, reject) => {

Check failure on line 105 in src/main.ts

View workflow job for this annotation

GitHub Actions / NPM lint

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

if (firstTry) {
firstTry = false
resolve(authenticatedConfig)
} else {
return axios.request(authenticatedConfig)
}

const { promise, resolve: subResolve, reject: subReject } = Promise.withResolvers()
resolvePwdDialogCallback = subResolve
rejectPwdDialogCallback = subReject

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

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

return response
})

return axios
return response
},
(error) => {
// TODO intercept reject to inform dialog
if (error.config.auth !== undefined) {
rejectPwdDialogCallback(error)
}
},
{
runWhen(config) {
return config.auth !== undefined
},
},
)

return pwdConfirmationAxios
}

0 comments on commit 5579613

Please sign in to comment.