@@ -7,29 +7,39 @@ import type { AxiosResponse } from '@nextcloud/axios'
77import type { Node } from '@nextcloud/files'
88import type { StorageConfig } from '../services/externalStorage'
99
10- import { generateOcsUrl , generateUrl } from '@nextcloud/router'
11- import { showError , showSuccess } from '@nextcloud/dialogs'
10+ import { generateUrl } from '@nextcloud/router'
11+ import { showError , showSuccess , spawnDialog } from '@nextcloud/dialogs'
1212import { translate as t } from '@nextcloud/l10n'
1313import axios from '@nextcloud/axios'
1414import LoginSvg from '@mdi/svg/svg/login.svg?raw'
15- import Vue from 'vue'
15+ import Vue , { defineAsyncComponent } from 'vue'
1616
1717import { FileAction , DefaultType } from '@nextcloud/files'
1818import { STORAGE_STATUS , isMissingAuthConfig } from '../utils/credentialsUtils'
1919import { isNodeExternalStorage } from '../utils/externalStorageUtils'
2020
21- type OCSAuthResponse = {
22- ocs : {
23- meta : {
24- status : string
25- statuscode : number
26- message : string
27- } ,
28- data : {
29- user ?: string ,
30- password ?: string ,
31- }
21+ type CredentialResponse = {
22+ login ?: string ,
23+ password ?: string ,
24+ }
25+
26+ async function setCredentials ( node : Node , login : string , password : string ) : Promise < null | true > {
27+ const configResponse = await axios . put ( generateUrl ( 'apps/files_external/userglobalstorages/{id}' , node . attributes ) , {
28+ backendOptions : { user : login , password } ,
29+ } ) as AxiosResponse < StorageConfig >
30+
31+ const config = configResponse . data
32+ if ( config . status !== STORAGE_STATUS . SUCCESS ) {
33+ showError ( t ( 'files_external' , 'Unable to update this external storage config. {statusMessage}' , {
34+ statusMessage : config ?. statusMessage || '' ,
35+ } ) )
36+ return null
3237 }
38+
39+ // Success update config attribute
40+ showSuccess ( t ( 'files_external' , 'New configuration successfully saved' ) )
41+ Vue . set ( node . attributes , 'config' , config )
42+ return true
3343}
3444
3545export const action = new FileAction ( {
@@ -57,30 +67,16 @@ export const action = new FileAction({
5767 } ,
5868
5969 async exec ( node : Node ) {
60- // always resolve auth request, we'll process the data afterwards
61- // Using fetch as axios have integrated auth handling and X-Requested-With header
62- const response = await fetch ( generateOcsUrl ( '/apps/files_external/api/v1/auth' ) , {
63- headers : new Headers ( { Accept : 'application/json' } ) ,
64- credentials : 'include' ,
65- } )
66-
67- const data = ( await response ?. json ( ) || { } ) as OCSAuthResponse
68- if ( data . ocs . data . user && data . ocs . data . password ) {
69- const configResponse = await axios . put ( generateUrl ( 'apps/files_external/userglobalstorages/{id}' , node . attributes ) , {
70- backendOptions : data . ocs . data ,
71- } ) as AxiosResponse < StorageConfig >
72-
73- const config = configResponse . data
74- if ( config . status !== STORAGE_STATUS . SUCCESS ) {
75- showError ( t ( 'files_external' , 'Unable to update this external storage config. {statusMessage}' , {
76- statusMessage : config ?. statusMessage || '' ,
77- } ) )
78- return null
79- }
70+ const { login, password } = await new Promise < CredentialResponse > ( resolve => spawnDialog (
71+ defineAsyncComponent ( ( ) => import ( '../views/CredentialsDialog.vue' ) ) ,
72+ { } ,
73+ ( args ) => {
74+ resolve ( args as CredentialResponse )
75+ } ,
76+ ) )
8077
81- // Success update config attribute
82- showSuccess ( t ( 'files_external' , 'New configuration successfully saved' ) )
83- Vue . set ( node . attributes , 'config' , config )
78+ if ( login && password ) {
79+ return await setCredentials ( node , login , password )
8480 }
8581
8682 return null
0 commit comments