@@ -24,29 +24,39 @@ import type { AxiosResponse } from 'axios'
2424import type { Node } from '@nextcloud/files'
2525import type { StorageConfig } from '../services/externalStorage'
2626
27- import { generateOcsUrl , generateUrl } from '@nextcloud/router'
28- import { showError , showSuccess } from '@nextcloud/dialogs'
27+ import { generateUrl } from '@nextcloud/router'
28+ import { showError , showSuccess , spawnDialog } from '@nextcloud/dialogs'
2929import { translate as t } from '@nextcloud/l10n'
3030import axios from '@nextcloud/axios'
3131import LoginSvg from '@mdi/svg/svg/login.svg?raw'
32- import Vue from 'vue'
32+ import Vue , { defineAsyncComponent } from 'vue'
3333
3434import { FileAction , DefaultType } from '@nextcloud/files'
3535import { STORAGE_STATUS , isMissingAuthConfig } from '../utils/credentialsUtils'
3636import { isNodeExternalStorage } from '../utils/externalStorageUtils'
3737
38- type OCSAuthResponse = {
39- ocs : {
40- meta : {
41- status : string
42- statuscode : number
43- message : string
44- } ,
45- data : {
46- user ?: string ,
47- password ?: string ,
48- }
38+ type CredentialResponse = {
39+ login ?: string ,
40+ password ?: string ,
41+ }
42+
43+ async function setCredentials ( node : Node , login : string , password : string ) : Promise < null | true > {
44+ const configResponse = await axios . put ( generateUrl ( 'apps/files_external/userglobalstorages/{id}' , node . attributes ) , {
45+ backendOptions : { user : login , password } ,
46+ } ) as AxiosResponse < StorageConfig >
47+
48+ const config = configResponse . data
49+ if ( config . status !== STORAGE_STATUS . SUCCESS ) {
50+ showError ( t ( 'files_external' , 'Unable to update this external storage config. {statusMessage}' , {
51+ statusMessage : config ?. statusMessage || '' ,
52+ } ) )
53+ return null
4954 }
55+
56+ // Success update config attribute
57+ showSuccess ( t ( 'files_external' , 'New configuration successfully saved' ) )
58+ Vue . set ( node . attributes , 'config' , config )
59+ return true
5060}
5161
5262export const action = new FileAction ( {
@@ -74,30 +84,16 @@ export const action = new FileAction({
7484 } ,
7585
7686 async exec ( node : Node ) {
77- // always resolve auth request, we'll process the data afterwards
78- // Using fetch as axios have integrated auth handling and X-Requested-With header
79- const response = await fetch ( generateOcsUrl ( '/apps/files_external/api/v1/auth' ) , {
80- headers : new Headers ( { Accept : 'application/json' } ) ,
81- credentials : 'include' ,
82- } )
83-
84- const data = ( await response ?. json ( ) || { } ) as OCSAuthResponse
85- if ( data . ocs . data . user && data . ocs . data . password ) {
86- const configResponse = await axios . put ( generateUrl ( 'apps/files_external/userglobalstorages/{id}' , node . attributes ) , {
87- backendOptions : data . ocs . data ,
88- } ) as AxiosResponse < StorageConfig >
89-
90- const config = configResponse . data
91- if ( config . status !== STORAGE_STATUS . SUCCESS ) {
92- showError ( t ( 'files_external' , 'Unable to update this external storage config. {statusMessage}' , {
93- statusMessage : config ?. statusMessage || '' ,
94- } ) )
95- return null
96- }
87+ const { login, password } = await new Promise < CredentialResponse > ( resolve => spawnDialog (
88+ defineAsyncComponent ( ( ) => import ( '../views/CredentialsDialog.vue' ) ) ,
89+ { } ,
90+ ( args ) => {
91+ resolve ( args as CredentialResponse )
92+ } ,
93+ ) )
9794
98- // Success update config attribute
99- showSuccess ( t ( 'files_external' , 'New configuration successfully saved' ) )
100- Vue . set ( node . attributes , 'config' , config )
95+ if ( login && password ) {
96+ return await setCredentials ( node , login , password )
10197 }
10298
10399 return null
0 commit comments