-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.vue
151 lines (138 loc) · 3.58 KB
/
index.vue
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
<template>
<main class="max-w-lg mx-auto items-center justify-center px-2">
<div class="bg-white px-6 py-8 rounded shadow-md text-black w-full">
<h1 class="mb-8 text-3xl text-center">Register</h1>
<label for="inputFirstName">
<input
id="inputFirstName"
v-model.trim="firstName"
type="text"
class="block border border-grey-light w-full p-3 rounded mb-4"
name="inputFirstName"
placeholder="First Name"
/>
</label>
<label for="inputLastName">
<input
id="inputLastName"
v-model.trim="lastName"
type="text"
class="block border border-grey-light w-full p-3 rounded mb-4"
name="inputLastName"
placeholder="Last Name"
/>
</label>
<label for="inputEmail">
<input
id="inputEmail"
v-model.trim="email"
type="email"
class="block border border-grey-light w-full p-3 rounded mb-4"
name="inputEmail"
placeholder="Email Address"
/>
</label>
<label for="inputUsername">
<input
id="inputUsername"
v-model.trim="username"
type="text"
class="block border border-grey-light w-full p-3 rounded mb-4"
name="inputUsername"
placeholder="Username"
/>
</label>
<label for="inputPassword">
<input
id="inputPassword"
v-model.trim="password"
type="password"
class="block border border-grey-light w-full p-3 rounded mb-4"
name="inputPassword"
placeholder="Password"
/>
</label>
<button
type="submit"
class="
w-full
text-center
py-3
rounded
bg-transparent
text-green-500
hover:text-white hover:bg-green-500
border border-green-500
hover:border-transparent
focus:outline-none
my-1
"
@click="register"
>
Register
</button>
</div>
</main>
</template>
<script lang="ts">
import {
defineComponent,
reactive,
toRefs,
useContext,
} from '@nuxtjs/composition-api'
export default defineComponent({
setup() {
const state = reactive({
firstName: '',
lastName: '',
email: '',
username: '',
password: '',
})
const { redirect, $axios, store, $toast } = useContext()
const validateField = () => {
if (
state.firstName === '' ||
state.lastName === '' ||
state.email === '' ||
state.username === '' ||
state.password === ''
) {
$toast.error('Please fill all the fields correctly.')
return false
}
if (!/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(state.email)) {
$toast.error('Please enter a valid email address.')
return false
}
return true
}
function register() {
if (!validateField()) return
const data = {
name: `${state.firstName} ${state.lastName}`,
email: state.email,
username: state.username,
password: state.password,
}
$toast.info('Please wait...')
$axios
.$post('auth/register/', data)
.then(({ token }) => {
store.commit('setToken', token)
redirect('/')
})
.catch(() => {
$toast.error(
'An account using same email or username is already created'
)
})
}
return {
...toRefs(state),
register,
}
},
})
</script>