aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/src/components/AuthTokenSetup.vue
blob: dcff95afd6343004f074b03402627ef83237ef7b (plain)
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
<!--
  - SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  - SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
	<form id="generate-app-token-section"
		class="row spacing"
		@submit.prevent="submit">
		<!-- Port to TextField component when available -->
		<NcTextField :value.sync="deviceName"
			type="text"
			:maxlength="120"
			:disabled="loading"
			class="app-name-text-field"
			:label="t('settings', 'App name')"
			:placeholder="t('settings', 'App name')" />
		<NcButton type="primary"
			:disabled="loading || deviceName.length === 0"
			native-type="submit">
			{{ t('settings', 'Create new app password') }}
		</NcButton>

		<AuthTokenSetupDialog :token="newToken" @close="newToken = null" />
	</form>
</template>

<script lang="ts">
import { showError } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { defineComponent } from 'vue'
import { useAuthTokenStore, type ITokenResponse } from '../store/authtoken'

import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'

import AuthTokenSetupDialog from './AuthTokenSetupDialog.vue'
import logger from '../logger'

export default defineComponent({
	name: 'AuthTokenSetup',
	components: {
		NcButton,
		NcTextField,
		AuthTokenSetupDialog,
	},
	setup() {
		const authTokenStore = useAuthTokenStore()
		return { authTokenStore }
	},
	data() {
		return {
			deviceName: '',
			loading: false,
			newToken: null as ITokenResponse|null,
		}
	},
	methods: {
		t,
		reset() {
			this.loading = false
			this.deviceName = ''
			this.newToken = null
		},
		async submit() {
			try {
				this.loading = true
				this.newToken = await this.authTokenStore.addToken(this.deviceName)
			} catch (error) {
				logger.error(error as Error)
				showError(t('settings', 'Error while creating device token'))
				this.reset()
			} finally {
				this.loading = false
			}
		},
	},
})
</script>

<style lang="scss" scoped>
	.app-name-text-field {
		height: 44px !important;
		padding-left: 12px;
		margin-right: 12px;
		width: 200px;
	}

	.row {
		display: flex;
		align-items: center;
	}

	.spacing {
		padding-top: 16px;
	}
</style>