-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathLogin.vue
More file actions
179 lines (175 loc) · 4.57 KB
/
Login.vue
File metadata and controls
179 lines (175 loc) · 4.57 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
<!--
# Copyright 2022 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2024, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
-->
<template>
<v-card>
<v-card-title> Login </v-card-title>
<v-card-subtitle>
{{ isSet ? 'Enter the' : 'Create a' }}
password to begin using OpenC3
</v-card-subtitle>
<v-card-text>
<v-form>
<v-text-field
v-if="isSet && reset"
v-model="oldPassword"
type="password"
label="Old Password"
/>
<v-text-field
v-model="password"
type="password"
:label="`${!isSet || reset ? 'New ' : ''}Password`"
data-test="new-password"
/>
<v-text-field
v-if="reset"
v-model="confirmPassword"
:rules="[rules.matchPassword]"
type="password"
label="Confirm Password"
data-test="confirm-password"
/>
<v-btn
v-if="reset"
type="submit"
@click.prevent="setPassword"
large
:color="isSet ? 'warn' : 'success'"
:disabled="!formValid"
data-test="set-password"
>
Set
</v-btn>
<v-container v-else>
<v-row>
<v-btn
type="submit"
@click.prevent="verifyPassword"
large
color="success"
:disabled="!formValid"
>
Login
</v-btn>
<v-spacer />
<v-btn text small @click="showReset"> Change Password </v-btn>
</v-row>
</v-container>
</v-form>
</v-card-text>
<v-alert :type="alertType" v-model="showAlert" dismissible>
{{ alert }}
</v-alert>
</v-card>
</template>
<script>
import Api from '../../../services/api'
export default {
data() {
return {
isSet: true,
password: '',
confirmPassword: '',
oldPassword: '',
reset: false, // setting a password for the first time, or changing to a new password
alert: '',
alertType: 'success',
showAlert: false,
}
},
computed: {
options: function () {
return {
noAuth: true,
noScope: true, // lol
}
},
rules: function () {
return {
matchPassword: () =>
this.password === this.confirmPassword || 'Passwords must match',
}
},
formValid: function () {
if (this.reset) {
if (!this.isSet) {
return !!this.password && this.password === this.confirmPassword
} else {
return (
!!this.oldPassword &&
!!this.password &&
this.password === this.confirmPassword
)
}
} else {
return !!this.password
}
},
},
created: function () {
Api.get('/openc3-api/auth/token-exists', this.options).then((response) => {
this.isSet = !!response.data.result
if (!this.isSet) {
this.reset = true
}
})
},
methods: {
showReset: function () {
this.reset = true
},
login: function () {
localStorage.openc3Token = this.password
const redirect = new URLSearchParams(window.location.search).get(
'redirect',
)
window.location = decodeURI(redirect || '/')
},
verifyPassword: function () {
this.showAlert = false
Api.post('/openc3-api/auth/verify', {
data: {
token: this.password,
},
...this.options,
})
.then((response) => {
this.login()
})
.catch((error) => {
this.alert = 'Incorrect password'
this.alertType = 'warning'
this.showAlert = true
})
},
setPassword: function () {
this.showAlert = false
Api.post('/openc3-api/auth/set', {
data: {
old_token: this.oldPassword,
token: this.password,
},
...this.options,
}).then(this.login)
},
},
}
</script>