-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCreateAccount.vue
More file actions
223 lines (212 loc) · 6.11 KB
/
CreateAccount.vue
File metadata and controls
223 lines (212 loc) · 6.11 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<template>
<v-form @submit="createaccount">
<v-card class="elevation-12" max-width="477">
<v-toolbar dark color="primary">
<v-toolbar-title>{{title}}</v-toolbar-title>
</v-toolbar>
<v-card-text>
<v-text-field
id="inputEmail"
prepend-icon="mdi-email"
name="login"
label="Email address"
type="email"
required
v-model="email"
:disabled="inFlight"
:error-messages="error['inputEmail']"
/>
<v-text-field
id="inputPassword"
prepend-icon="mdi-lock"
name="password"
label="Password"
type="password"
required
hint="Your password must be at least 8 characters long."
v-model="password"
:disabled="inFlight"
:error-messages="error['inputPassword']"
/>
<v-text-field
id="inputPasswordConfirmation"
prepend-icon="mdi-lock"
name="passwordConfirmation"
label="Confirm Password"
type="password"
required
v-model="passwordConfirmation"
:disabled="inFlight"
:error-messages="error['inputPasswordConfirmation']"
/>
<v-checkbox
required
v-model="terms"
:disabled="inFlight"
:error-messages="error['terms']"
>
<template v-slot:label>
<div>
I agree to the
<v-tooltip bottom>
<template v-slot:activator="{ on }">
<a
target="_blank"
href="/terms-of-use"
@click.stop
v-on="on"
>
Terms of Use.
</a>
</template>
Opens in new window
</v-tooltip>
</div>
</template>
</v-checkbox>
<p>
This site is protected by reCAPTCHA and the Google
<a target="_blank" href="https://policies.google.com/privacy">Privacy Policy</a> and
<a target="_blank" href="https://policies.google.com/terms">Terms of Service</a> apply.
</p>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
type="submit"
color="primary"
:disabled="inFlight"
>
{{buttonText}}
</v-btn>
</v-card-actions>
</v-card>
</v-form>
</template>
<script>
export default {
name: 'CreateAccountCard',
props: [
'title',
'buttonText',
],
components: {},
computed: {
isLoggedIn: function () {
return this.$store.getters.isLoggedIn
},
},
data () {
return {
email: '',
password: '',
passwordConfirmation: '',
terms: false,
hasError: false,
error: [],
inFlight: false,
}
},
created () {
this.checkCurrentLogin()
},
updated () {
this.checkCurrentLogin()
},
methods: {
resetErrorState () {
this.hasError = false
this.error = []
},
setGeneralErrorState (error) {
error = error || 'Something went wrong'
this.resetErrorState()
this.hasError = true
this.error.inputEmail = error
this.error.inputPassword = error
this.error.inputPasswordConfirmation = error
this.error.terms = error
this.inFlight = false
},
createaccount (evt) {
evt.preventDefault()
// Request is processing
this.inFlight = true
// Reset errors
this.resetErrorState()
// Check for the terms
if (this.terms === false) {
this.hasError = true
this.error.terms = 'You must accept the Terms of Service.'
}
// Check for matching confirmed password
if (this.password !== this.passwordConfirmation) {
this.hasError = true
this.error.inputPassword = 'Passwords do not match.'
this.error.inputPasswordConfirmation = 'Passwords do not match.'
}
// If the error are not empty then dont submit the request
if (this.hasError) {
this.inFlight = false
return
}
// TODO once emailing is setup add emailVerificationRequired option to user model in model-config.json
// TODO recaptcha check should be optional for development (env var switch?)
// Recaptcha check
this.$recaptcha('login').then((token) => {
this.$api.register(
{
email: this.email,
password: this.password,
recaptcha: token,
})
.then(success => this.createSuccessful(success))
.catch(errors => {
this.resetErrorState()
// If the api gave use details of the error, then use them
if (errors) {
if (errors.email) {
this.hasError = true
this.error.inputEmail = errors.email[0]
}
if (errors.password) {
this.hasError = true
this.error.inputPassword = errors.password[0]
}
}
// Otherwise show a general error state
if (!this.hasError) {
this.setGeneralErrorState(errors)
}
this.$store.dispatch('logout')
this.inFlight = false
})
})
},
createSuccessful (success) {
if (!success) {
this.setGeneralErrorState()
return
}
const email = this.email
const password = this.password
this.$store
.dispatch('login', { email, password })
.then(() => this.$router.push('/dashboard'))
.catch(err => {
console.log(err)
// TODO better error messages..
this.setGeneralErrorState('Post account creation authentication failed!')
this.inFlight = false
})
},
checkCurrentLogin () {
if (this.isLoggedIn) {
this.$router.replace(this.$route.query.redirect || '/dashboard')
}
},
},
}
</script>
<style lang="css" scoped>
</style>