aboutsummaryrefslogtreecommitdiff
path: root/src/site/pages/login.vue
blob: 197426376661e89cc95898f600c60b5ebae3b6e7 (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
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
<template>
	<section class="section is-fullheight is-login">
		<div class="container">
			<h1 class="title">
				Dashboard Access
			</h1>
			<h2 class="subtitle mb5">
				Login to access your files and folders
			</h2>
			<div class="columns">
				<div class="column is-4 is-offset-4">
					<b-field>
						<b-input
							v-model="username"
							class="chibisafe-input"
							type="text"
							placeholder="Username"
							@keyup.enter.native="login" />
					</b-field>
					<b-field>
						<b-input
							v-model="password"
							class="chibisafe-input"
							type="password"
							placeholder="Password"
							password-reveal
							@keyup.enter.native="login" />
					</b-field>

					<p class="control has-addons is-pulled-right" />

					<div class="level">
						<div class="level-left">
							<div class="level-item">
								<router-link
									v-if="config.userAccounts"
									to="/register"
									class="is-text">
									Don't have an account?
								</router-link>
								<span v-else>Registration is closed at the moment</span>
							</div>
						</div>

						<div class="level-right">
							<p class="level-item">
								<b-button
									size="is-medium"
									type="is-chibisafe"
									@click="login">
									Login
								</b-button>
							</p>
						</div>
					</div>
				</div>
			</div>
		</div>

		<!--
		<b-modal :active.sync="isMfaModalActive"
			:canCancel="true"
			has-modal-card>
			<div class="card mfa">
				<div class="card-content">
					<div class="content">
						<p>Enter your Two-Factor code to proceed.</p>
						<b-field>
							<b-input v-model="mfaCode"
								placeholder="Your MFA Code"
								type="text"
								@keyup.enter.native="mfa"/>
							<p class="control">
								<button :class="{ 'is-loading': isLoading }"
									class="button is-primary"
									@click="mfa">Submit</button>
							</p>
						</b-field>
					</div>
				</div>
			</div>
		</b-modal>
		-->
	</section>
</template>

<script>
import { mapState } from 'vuex';

export default {
	name: 'Login',
	data() {
		return {
			username: null,
			password: null,
			mfaCode: null,
			isMfaModalActive: false,
			isLoading: false
		};
	},
	computed: mapState(['config', 'auth']),
	metaInfo() {
		return { title: 'Login' };
	},
	created() {
		if (this.auth.loggedIn) {
			this.redirect();
		}
	},
	methods: {
		async login() {
			if (this.isLoading) return;

			const { username, password } = this;
			if (!username || !password) {
				this.$notifier.error('Please fill both fields before attempting to log in.');
				return;
			}

			try {
				this.isLoading = true;
				await this.$store.dispatch('auth/login', { username, password });
				if (this.auth.loggedIn) {
					this.redirect();
				}
			} catch (e) {
				this.$notifier.error(e.message);
			} finally {
				this.isLoading = false;
			}
		},
		/*
		mfa() {
			if (!this.mfaCode) return;
			if (this.isLoading) return;
			this.isLoading = true;
			this.axios.post(`${this.$BASE_URL}/login/mfa`, { token: this.mfaCode })
				.then(res => {
					this.$store.commit('token', res.data.token);
					this.redirect();
				})
				.catch(err => {
					this.isLoading = false;
					this.$onPromiseError(err);
				});
		}, */
		redirect() {
			if (typeof this.$route.query.redirect !== 'undefined') {
				this.$router.push(this.$route.query.redirect);
				return;
			}
			this.$router.push('/dashboard');
		}
	}
};
</script>