mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: add error handling for choose username
This commit is contained in:
1 parent
20225093c6
commit
cbe7adc3f8
4 files changed
+77
-44
No files matched your search
+46
-8
@@ -17,18 +17,22 @@ export const login = async (username, password) => {
|
|||||||
await api.post('/auth/login', { username, password })
|
await api.post('/auth/login', { username, password })
|
||||||
return true
|
return true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.response?.status === 401) {
|
const { status } = err.response
|
||||||
|
switch (status) {
|
||||||
|
case 401:
|
||||||
throw new UnauthorizedError(err.response.data.message)
|
throw new UnauthorizedError(err.response.data.message)
|
||||||
}
|
|
||||||
if (err.response?.status === 429) {
|
case 429:
|
||||||
throw new TooManyRequestsError(err.response.data.message)
|
throw new TooManyRequestsError(err.response.data.message)
|
||||||
}
|
|
||||||
if (err.response?.status === 400) {
|
case 400:
|
||||||
throw new ValidationError(err.response.data.message)
|
throw new ValidationError(err.response.data.message)
|
||||||
}
|
|
||||||
|
default:
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const register = async (username, email, password) => {
|
export const register = async (username, email, password) => {
|
||||||
try {
|
try {
|
||||||
@@ -39,7 +43,7 @@ export const register = async (username, email, password) => {
|
|||||||
})
|
})
|
||||||
return true
|
return true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const status = err.response?.status
|
const { status } = err.response
|
||||||
if (status === 429) {
|
if (status === 429) {
|
||||||
throw new TooManyRequestsError(err.response.data.message)
|
throw new TooManyRequestsError(err.response.data.message)
|
||||||
}
|
}
|
||||||
@@ -48,7 +52,7 @@ export const register = async (username, email, password) => {
|
|||||||
}
|
}
|
||||||
if (status === 400) {
|
if (status === 400) {
|
||||||
const { errors } = err.response.data
|
const { errors } = err.response.data
|
||||||
if (errors?.length > 0) {
|
if (Array.isArray(errors) && errors.length > 0) {
|
||||||
const { path, msg } = errors[0]
|
const { path, msg } = errors[0]
|
||||||
switch (path) {
|
switch (path) {
|
||||||
case 'username':
|
case 'username':
|
||||||
@@ -61,11 +65,45 @@ export const register = async (username, email, password) => {
|
|||||||
throw new ValidationError(msg)
|
throw new ValidationError(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw new ValidationError(err.response.data.message)
|
||||||
}
|
}
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const checkUsername = async (username) => {
|
||||||
|
try {
|
||||||
|
await api.get('/auth/check-username', {
|
||||||
|
params: { username },
|
||||||
|
})
|
||||||
|
return true
|
||||||
|
} catch (err) {
|
||||||
|
const { status } = err.response
|
||||||
|
if (status === 429) {
|
||||||
|
throw new TooManyRequestsError('Too many requests')
|
||||||
|
}
|
||||||
|
if (status === 409) {
|
||||||
|
throw new ValidationError('Username is already taken')
|
||||||
|
}
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const chooseUsername = async (username) => {
|
||||||
|
try {
|
||||||
|
await api.post('/auth/register-from-session', { username })
|
||||||
|
} catch (err) {
|
||||||
|
switch (err.response?.status) {
|
||||||
|
case 400:
|
||||||
|
throw new ValidationError('Username is already taken')
|
||||||
|
case 401:
|
||||||
|
throw new UnauthorizedError('You are not a new user')
|
||||||
|
default:
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const logout = () => {
|
export const logout = () => {
|
||||||
return api.post('/auth/logout')
|
return api.post('/auth/logout')
|
||||||
}
|
}
|
||||||
@@ -2,44 +2,46 @@
|
|||||||
import { isValidUsername } from '@linux-odyssey/utils'
|
import { isValidUsername } from '@linux-odyssey/utils'
|
||||||
import AuthForm from '../components/AuthForm.vue'
|
import AuthForm from '../components/AuthForm.vue'
|
||||||
import Background from '../components/DynamicBackground.vue'
|
import Background from '../components/DynamicBackground.vue'
|
||||||
|
import { TooManyRequestsError, ValidationError } from '../utils/errors'
|
||||||
|
import { checkUsername, chooseUsername } from '../utils/auth'
|
||||||
|
|
||||||
import api from '../utils/api'
|
async function check({ username, error }) {
|
||||||
|
|
||||||
async function checkUsername({ username, error }) {
|
|
||||||
if (username && !isValidUsername(username)) {
|
if (username && !isValidUsername(username)) {
|
||||||
error('Invalid username.')
|
error('Invalid username.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (username.length > 3) {
|
if (username.length > 3) {
|
||||||
try {
|
try {
|
||||||
const { data } = await api.get('/auth/check-username', {
|
await checkUsername(username)
|
||||||
params: { username },
|
} catch (err) {
|
||||||
})
|
if (err instanceof TooManyRequestsError) {
|
||||||
|
error('Too many requests. Try again in 2 minutes.')
|
||||||
const { available } = data
|
return
|
||||||
if (!available) {
|
|
||||||
error('Username is already taken.')
|
|
||||||
}
|
}
|
||||||
} catch {
|
if (err instanceof ValidationError) {
|
||||||
error('Invalid username.')
|
error(err.message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
error('Something went wrong. Please try again later.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSubmit({ username, success, error }) {
|
async function handleSubmit({ username, success, error }) {
|
||||||
api
|
try {
|
||||||
.post('/auth/register-from-session', {
|
await chooseUsername(username)
|
||||||
username,
|
success()
|
||||||
})
|
} catch (err) {
|
||||||
.then(success)
|
if (err instanceof TooManyRequestsError) {
|
||||||
.catch((err) => {
|
error('Too many requests. Try again in 2 minutes.')
|
||||||
if (err.response?.status === 409) {
|
return
|
||||||
error('Username is already taken.')
|
}
|
||||||
} else {
|
if (err instanceof ValidationError) {
|
||||||
console.error(err.message)
|
error(err.message)
|
||||||
error('Something went wrong.')
|
return
|
||||||
|
}
|
||||||
|
error(err.message)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
@@ -51,7 +53,7 @@ function handleSubmit({ username, success, error }) {
|
|||||||
<div class="h-2/3 w-1/3">
|
<div class="h-2/3 w-1/3">
|
||||||
<AuthForm
|
<AuthForm
|
||||||
@onSubmit="handleSubmit"
|
@onSubmit="handleSubmit"
|
||||||
@onChange="checkUsername"
|
@onChange="check"
|
||||||
type="username"
|
type="username"
|
||||||
:socialLogin="false"
|
:socialLogin="false"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import { passwordPolicy } from '@linux-odyssey/constants'
|
|||||||
import AuthForm from '../components/AuthForm.vue'
|
import AuthForm from '../components/AuthForm.vue'
|
||||||
import Background from '../components/DynamicBackground.vue'
|
import Background from '../components/DynamicBackground.vue'
|
||||||
|
|
||||||
import api from '../utils/api'
|
import { checkUsername, register } from '../utils/auth'
|
||||||
import { register } from '../utils/auth'
|
|
||||||
import {
|
import {
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
TooManyRequestsError,
|
TooManyRequestsError,
|
||||||
@@ -52,13 +51,7 @@ async function check({ username, email, password, error }) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const res = await api.get('/auth/check-username', {
|
await checkUsername(username)
|
||||||
params: { username },
|
|
||||||
})
|
|
||||||
const { available } = res.data
|
|
||||||
if (!available) {
|
|
||||||
error(`Username already exists.`)
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.response?.status === 400) {
|
if (err.response?.status === 400) {
|
||||||
error('Invalid username.')
|
error('Invalid username.')
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ export async function registerFromSession(req, res) {
|
|||||||
const { newUser } = req.session
|
const { newUser } = req.session
|
||||||
|
|
||||||
if (!newUser) {
|
if (!newUser) {
|
||||||
res.status(400).json({
|
res.status(401).json({
|
||||||
message: 'no new user found',
|
message: 'no new user found',
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in new issue
Block a user