aboutsummaryrefslogtreecommitdiff
path: root/src/site/pages/dashboard/account.vue
blob: 6ecc885e609692c25070faf8e033af7563efdc06 (plain) (blame)
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
<template>
	<section class="hero is-fullheight dashboard">
		<div class="hero-body">
			<div class="container">
				<div class="columns">
					<div class="column is-narrow">
						<Sidebar />
					</div>
					<div class="column">
						<h2 class="subtitle">Account settings</h2>
						<hr>

						<b-field label="Username"
							message="Nothing to do here"
							horizontal>
							<b-input v-model="user.username"
								expanded
								disabled />
						</b-field>

						<b-field label="Current password"
							message="If you want to change your password input the current one here"
							horizontal>
							<b-input v-model="user.password"
								type="password"
								expanded />
						</b-field>

						<b-field label="New password"
							message="Your new password"
							horizontal>
							<b-input v-model="user.newPassword"
								type="password"
								expanded />
						</b-field>

						<b-field label="New password again"
							message="Your new password once again"
							horizontal>
							<b-input v-model="user.reNewPassword"
								type="password"
								expanded />
						</b-field>

						<div class="mb2 mt2 text-center">
							<button class="button is-primary"
								@click="changePassword">Change password</button>
						</div>

						<b-field label="Api key"
							message="This API key lets you use the service from other apps"
							horizontal>
							<b-input v-model="user.apiKey"
								expanded
								disabled />
						</b-field>

						<div class="mb2 mt2 text-center">
							<button class="button is-primary"
								@click="promptNewAPIKey">Request new API key</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>
</template>

<script>
import Sidebar from '~/components/sidebar/Sidebar.vue';

export default {
	components: {
		Sidebar
	},
	middleware: 'auth',
	data() {
		return {
			user: {}
		};
	},
	metaInfo() {
		return { title: 'Account' };
	},
	mounted() {
		this.getUserSetttings();
	},
	methods: {
		async getUserSetttings() {
			const response = await this.$axios.$get(`users/me`);
			this.user = response.user;
		},
		async changePassword() {
			if (!this.user.password || !this.user.newPassword || !this.user.reNewPassword) {
				this.$store.dispatch('alert', {
					text: 'One or more fields are missing',
					error: true
				});
				return;
			}
			if (this.user.newPassword !== this.user.reNewPassword) {
				this.$store.dispatch('alert', {
					text: 'Passwords don\'t match',
					error: true
				});
				return;
			}

			const response = await this.$axios.$post(`user/password/change`,
				{
					password: this.user.password,
					newPassword: this.user.newPassword
				});
			this.$buefy.toast.open(response.message);
		},
		promptNewAPIKey() {
			this.$buefy.dialog.confirm({
				type: 'is-danger',
				message: 'Are you sure you want to regenerate your API key? Previously generated API keys will stop working. Make sure to write the new key down as this is the only time it will be displayed to you.',
				onConfirm: () => this.requestNewAPIKey()
			});
		},
		async requestNewAPIKey() {
			const response = await this.$axios.$post(`user/apikey/change`);
			this.user.apiKey = response.apiKey;
			this.$forceUpdate();
			this.$buefy.toast.open(response.message);
		}
	}
};
</script>