feat: complete register validation

* add password policy in frontend
This commit is contained in:
wancat committed 2023-11-01 21:02:01 +08:00
1 parent 1c9d69a8e0
commit fc6ac11c60
11 files changed
+57 -23

No files matched your search

+3
View File
@@ -128,6 +128,7 @@ const handleChange = () => {
class="my-4 bg-background-primary text-text-primary bg- rounded-md block w-full px-3 h-10 shadow-sm focus:outline-none placeholder:text-text-line focus:ring-2 focus:ring-text-primary ring-1 ring-background-secondary"
:placeholder="type === 'login' ? 'Email / Username' : 'Username'"
v-model="username"
required
@input="handleChange()"
autocomplete="username"
/>
@@ -147,6 +148,7 @@ const handleChange = () => {
class="my-4 bg-background-primary text-text-primary bg- rounded-md block w-full px-3 h-10 shadow-sm focus:outline-none placeholder:text-text-line focus:ring-2 focus:ring-text-primary ring-1 ring-background-secondary"
placeholder="Email"
v-model="email"
required
@input="handleChange()"
autocomplete="email"
/>
@@ -158,6 +160,7 @@ const handleChange = () => {
class="my-4 bg-background-primary text-text-primary bg- rounded-md block w-full px-3 h-10 shadow-sm focus:outline-none placeholder:text-text-line focus:ring-2 focus:ring-text-primary ring-1 ring-background-secondary"
placeholder="Password"
v-model="password"
required
@input="handleChange()"
/>
</div>
+15 -7
View File
@@ -1,5 +1,7 @@
<script setup>
import { isValidEmail, isValidUsername } from '@linux-odyssey/utils'
import validator from 'validator'
import { isValidUsername } from '@linux-odyssey/utils'
import { passwordPolicy } from '@linux-odyssey/constants'
import AuthForm from '../components/AuthForm.vue'
import Background from '../components/DynamicBackground.vue'
@@ -27,29 +29,35 @@ function handleRegister({ username, email, password, success, error }) {
})
}
async function check({ username, email, error }) {
async function check({ username, email, password, error }) {
if (username && !isValidUsername(username)) {
error('Invalid username.')
return
}
if (email && !isValidEmail(email)) {
if (email && !validator.isEmail(email)) {
error('Invalid email.')
return
}
if (!username && !email) {
if (password && !validator.isStrongPassword(password, passwordPolicy)) {
error(
'Your password must be 8+ characters with at least one number, one upper and one lower case letter.'
)
return
}
if (!(username && email && password)) {
return
}
try {
const res = await api.get('/auth/check-username', {
params: { username },
})
const { type, available } = res.data
const { available } = res.data
if (!available) {
error(`${type} already exists.`)
error(`Username already exists.`)
}
} catch (err) {
if (err.response?.status === 400) {
error('Invalid username or email.')
error('Invalid username.')
return
}
console.error(err)