mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
refactor: simplify register flow
This commit is contained in:
1 parent
5dd56308ce
commit
a6d02f2616
3 files changed
+32
-36
No files matched your search
@@ -57,25 +57,9 @@ async function check() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSubmit() {
|
function handleRegister() {
|
||||||
if (!isRegister.value) {
|
|
||||||
check()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// register
|
|
||||||
console.log('register', username.value, email.value, password.value)
|
console.log('register', username.value, email.value, password.value)
|
||||||
api
|
register(username.value, email.value, password.value)
|
||||||
.get('/auth/check', {
|
|
||||||
params: { username: username.value },
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
// Username is available, now check if the email already exists
|
|
||||||
return api.get('/auth/check', { params: { email: email.value } })
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
// Both username and email are available, proceed with registration
|
|
||||||
return register(username.value, email.value, password.value)
|
|
||||||
})
|
|
||||||
.then((isSuccess) => {
|
.then((isSuccess) => {
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
success()
|
success()
|
||||||
@@ -102,6 +86,15 @@ async function handleSubmit() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleSubmit() {
|
||||||
|
if (!isRegister.value) {
|
||||||
|
check()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// register
|
||||||
|
handleRegister()
|
||||||
|
}
|
||||||
|
|
||||||
const clearError = () => {
|
const clearError = () => {
|
||||||
errorMessage.value = ''
|
errorMessage.value = ''
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-15
@@ -41,21 +41,14 @@ export const login = async (username, password) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const register = async (username, email, password) => {
|
export const register = async (username, email, password) => {
|
||||||
try {
|
const res = await api.post('/auth/register', {
|
||||||
const res = await api.post('/auth/register', {
|
username,
|
||||||
username,
|
password,
|
||||||
password,
|
email,
|
||||||
email,
|
})
|
||||||
})
|
const { token } = res.data
|
||||||
const { token } = res.data
|
myToken.value = token
|
||||||
myToken.value = token
|
return true
|
||||||
return true
|
|
||||||
} catch (err) {
|
|
||||||
if (err.response?.status === 409) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const logout = () => {
|
export const logout = () => {
|
||||||
|
|||||||
@@ -67,13 +67,23 @@ export async function register(req, res, next) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (await User.exists({ $or: [{ username }, { email }] })) {
|
|
||||||
|
if (await User.exists({ username })) {
|
||||||
res.status(409).json({
|
res.status(409).json({
|
||||||
message: 'username or email already exists',
|
type: 'username',
|
||||||
|
message: 'username already exists',
|
||||||
})
|
})
|
||||||
console.log(await User.find({ $or: [{ username }, { email }] }))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (await User.exists({ email })) {
|
||||||
|
res.status(409).json({
|
||||||
|
type: 'email',
|
||||||
|
message: 'username already exists',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const user = new User({
|
const user = new User({
|
||||||
username,
|
username,
|
||||||
email,
|
email,
|
||||||
|
|||||||
Reference in new issue
Block a user