refactor: AuthorizationService.kt

This commit is contained in:
SOR
2026-07-27 12:59:04 +08:00
parent 531659a891
commit 350c6dde67
3 changed files with 33 additions and 37 deletions
@@ -66,7 +66,7 @@ class AuthorizeController(
return service return service
.jwtTokenRelogin(authorization) .jwtTokenRelogin(authorization)
?.map { ApiResponse.success(it) } ?.map { ApiResponse.success(it) }
?: Mono.just(messageHandler("Please login with password.")) ?: Mono.just(ApiResponse.failure(401, null, "Please login with password."))
} }
private fun messageHandler(wrongMessage: String): ApiResponse<Any?> { private fun messageHandler(wrongMessage: String): ApiResponse<Any?> {
@@ -57,9 +57,6 @@ class AccountServiceImpl : ServiceImpl<AccountMapper, Account>(), AccountService
): Mono<T> = ): Mono<T> =
Mono.fromCallable(block) Mono.fromCallable(block)
.subscribeOn(Schedulers.boundedElastic()) .subscribeOn(Schedulers.boundedElastic())
.flatMap {
Mono.just(it)
}
private fun findAccount( private fun findAccount(
fieldName: String, fieldName: String,
@@ -54,40 +54,39 @@ class AuthorizationService(
val (email, code, username, password) = vo val (email, code, username, password) = vo
return verifyCode(email, code) return verifyCode(email, code)
.filter { it.isBlank() } .flatMap { result ->
.switchIfEmpty(Mono.just("wrong code")) if (result.isNotBlank()) {
.flatMap { return@flatMap Mono.just(result)
accountService.existAccountByEmail(email)
}
.flatMap { emailExists ->
if (emailExists) {
Mono.just("account with the same email already exists.")
} else {
accountService.existAccountByName(username).flatMap { usernameExists ->
if (usernameExists) {
Mono.just("username already exists.")
} else {
val account = Account(
id = null,
username = username,
password = encoder.encode(password)!!,
email = email,
role = "user",
registerTime = getCurrentDateTime()
)
Mono.fromCallable { accountService.save(account) }
.flatMap { success ->
if (success) {
stringRedisTemplate.delete(Const.VERIFY_EMAIL_DATA + email)
.thenReturn("")
} else {
Const.INTERNAL_ERROR_MONO
}
}
}
}
} }
accountService.existAccountByEmail(email)
.flatMap { emailExists ->
if (emailExists) {
return@flatMap Mono.just("account with the same email already exists.")
}
accountService.existAccountByName(username)
.flatMap { usernameExists ->
if (usernameExists) {
return@flatMap Mono.just("username already exists.")
}
val account = Account(
id = null,
username = username,
password = encoder.encode(password)!!,
email = email,
role = "user",
registerTime = getCurrentDateTime()
)
Mono.fromCallable { accountService.save(account) }
.flatMap { success ->
if (success) {
stringRedisTemplate.delete(Const.VERIFY_EMAIL_DATA + email)
.thenReturn("")
} else {
Const.INTERNAL_ERROR_MONO
}
}
}
}
} }
} }