-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathModalTokenManager.vue
More file actions
204 lines (186 loc) · 5.06 KB
/
ModalTokenManager.vue
File metadata and controls
204 lines (186 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!--
- SPDX-FileCopyrightText: 2024 LibreCode coop and LibreCode contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcDialog v-if="signMethodsStore.modal.sms"
:name="t('libresign', 'Sign with your phone number.')"
@closing="signMethodsStore.closeModal('sms')">
<div v-if="tokenRequested" class="code-request">
<h3 class="phone">
{{ newPhoneNumber }}
</h3>
<NcTextField v-model="token"
:disabled="loading"
name="code"
type="text" />
</div>
<div v-else class="store-number">
<NcTextField v-model="newPhoneNumber"
:disabled="loading"
name="cellphone"
placeholder="+55 00 0 0000 0000"
type="tel"
@change="sanitizeNumber" />
</div>
<template #actions>
<NcButton v-if="!tokenRequested" :disabled="loading || newPhoneNumber.length < 10" @click="requestCode">
<template #icon>
<NcLoadingIcon v-if="loading" :size="20" />
</template>
{{ t('libresign', 'Request code.') }}
</NcButton>
<NcButton v-else :disabled="loading || token.length < 3" @click="sendCode">
<template #icon>
<NcLoadingIcon v-if="loading" :size="20" />
</template>
{{ t('libresign', 'Send code.') }}
</NcButton>
</template>
</NcDialog>
</template>
<script>
import axios from '@nextcloud/axios'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { confirmPassword } from '@nextcloud/password-confirmation'
import { generateOcsUrl } from '@nextcloud/router'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcDialog from '@nextcloud/vue/components/NcDialog'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import NcTextField from '@nextcloud/vue/components/NcTextField'
import { settingsService } from '../../../domains/settings/index.js'
import { useSignStore } from '../../../store/sign.js'
import { useSignMethodsStore } from '../../../store/signMethods.js'
const sanitizeNumber = val => {
val = val.replace(/\D/g, '')
return `+${val}`
}
export default {
name: 'ModalTokenManager',
components: {
NcDialog,
NcLoadingIcon,
NcTextField,
NcButton,
},
props: {
phoneNumber: {
type: String,
required: false,
default: '',
},
},
setup() {
const signStore = useSignStore()
const signMethodsStore = useSignMethodsStore()
return { signStore, signMethodsStore }
},
data() {
return {
token: '',
newPhoneNumber: this.phoneNumber || '',
tokenRequested: false,
loading: false,
}
},
computed: {
activeTokenMethod() {
const tokenMethods = ['sms', 'whatsapp', 'signal', 'telegram', 'xmpp']
return tokenMethods.find(method =>
Object.hasOwn(this.signMethodsStore.settings, method)
) || 'sms'
},
activeIdentifyMethod() {
return this.activeTokenMethod
},
},
methods: {
async saveNumber() {
this.loading = true
this.sanitizeNumber()
await this.$nextTick()
try {
await confirmPassword()
const { data: { phone }, success } = await settingsService.saveUserNumber(this.newPhoneNumber)
this.newPhoneNumber = phone
this.$emit('update:phone', phone)
if (!success) {
showError(t('libresign', 'Review the entered number.'))
return
}
showSuccess(t('libresign', 'Phone stored.'))
} catch (err) {
showError(err.response.data.ocs.data.message)
} finally {
this.loading = false
}
},
async requestCode() {
this.loading = true
this.tokenRequested = false
await this.$nextTick()
try {
const params = {
identifyMethod: this.activeIdentifyMethod,
signMethod: this.activeTokenMethod,
}
if (this.signStore.document.fileId) {
const { data } = await axios.post(
generateOcsUrl('/apps/libresign/api/v1/sign/file_id/{fileId}/code', {
fileId: this.signStore.document.fileId,
}),
params
)
showSuccess(data.ocs.data.message)
} else {
const signer = this.signStore.document.signers.find(row => row.me) || {}
const { data } = await axios.post(
generateOcsUrl('/apps/libresign/api/v1/sign/uuid/{uuid}/code', {
uuid: signer.sign_uuid,
}),
params
)
showSuccess(data.ocs.data.message)
}
this.tokenRequested = true
} catch (err) {
const errorMessage = err.response?.data?.ocs?.data?.message || err.response?.data?.message || err.message
if (errorMessage && errorMessage.includes('Invalid configuration')) {
const method = this.activeTokenMethod.charAt(0).toUpperCase() + this.activeTokenMethod.slice(1)
showError(t('libresign', '{method} is not configured. Please contact your administrator.', { method }))
} else {
showError(errorMessage)
}
} finally {
this.loading = false
}
},
sendCode() {
this.$emit('change', this.token)
this.$nextTick(() => {
this.close()
})
},
close() {
this.$emit('close')
},
sanitizeNumber() {
this.newPhoneNumber = sanitizeNumber(this.newPhoneNumber)
},
},
}
</script>
<style lang="scss" scoped>
h3.phone {
font-family: monospace;
text-align: center;
}
.code-request, .store-number {
width: 100%;
display: flex;
flex-direction: column;
}
.code-request input {
font-size: 1.3em;
}
</style>