Merge branch 'main' into codex/remove-duplicate-notifications-for-register-request

This commit is contained in:
Tim
2025-07-15 12:52:21 +08:00
committed by GitHub
6 changed files with 31 additions and 10 deletions

View File

@@ -16,7 +16,7 @@ export async function googleGetIdToken() {
})
}
export async function googleAuthWithToken(idToken, reason, redirect) {
export async function googleAuthWithToken(idToken, reason, redirect_success, redirect_not_approved) {
try {
const res = await fetch(`${API_BASE_URL}/api/auth/google`, {
method: 'POST',
@@ -28,20 +28,24 @@ export async function googleAuthWithToken(idToken, reason, redirect) {
setToken(data.token)
await loadCurrentUser()
toast.success('登录成功')
if (redirect) redirect()
if (redirect_success) redirect_success()
} else if (data.reason_code === 'NOT_APPROVED') {
toast.info('您的注册理由正在审批中')
if (redirect) redirect()
toast.info('当前为注册审核模式,请填写注册理由')
sessionStorage.setItem('google_id_token', idToken)
if (redirect_not_approved) redirect_not_approved()
} else if (data.reason_code === 'IS_APPROVING') {
toast.info('您的注册理由正在审批中')
if (redirect_success) redirect_success()
}
} catch (e) {
toast.error('登录失败')
}
}
export async function googleSignIn(redirect, reason) {
export async function googleSignIn(redirect_success, redirect_not_approved) {
try {
const token = await googleGetIdToken()
await googleAuthWithToken(token, reason, redirect)
await googleAuthWithToken(token, '', redirect_success, redirect_not_approved)
} catch {
/* ignore */
}

View File

@@ -88,6 +88,8 @@ export default {
loginWithGoogle() {
googleSignIn(() => {
this.$router.push('/')
}, () => {
this.$router.push('/signup-reason?google=1')
})
}
}

View File

@@ -204,6 +204,8 @@ export default {
} else {
googleSignIn(() => {
this.$router.push('/')
}, () => {
this.$router.push('/signup-reason')
})
}
}

View File

@@ -54,7 +54,10 @@ export default {
toast.error('Google 登录失败')
return
}
await googleAuthWithToken(token, this.reason, () => { this.$router.push('/') })
await googleAuthWithToken(token, this.reason,
() => { this.$router.push('/') },
() => { this.error = 'Google 登录失败' }
)
this.isWaitingForRegister = false
sessionStorage.removeItem('google_id_token')
return
@@ -138,5 +141,4 @@ export default {
.signup-page-button-primary.disabled:hover {
cursor: not-allowed;
}
</style>

View File

@@ -93,9 +93,14 @@ public class AuthController {
Optional<User> user = googleAuthService.authenticate(req.getIdToken(), req.getReason(), registerModeService.getRegisterMode());
if (user.isPresent()) {
if (!user.get().isApproved()) {
if (req.getReason() != null && !req.getReason().isEmpty()) {
// do not send empty notification (while try login)
if (user.get().getRegisterReason() != null && !user.get().getRegisterReason().isEmpty()) {
// do not send empty notifition (while try login)
notificationService.createRegisterRequestNotifications(user.get(), req.getReason());
return ResponseEntity.badRequest().body(Map.of(
"error", "Account awaiting approval",
"reason_code", "IS_APPROVING"
));
}
return ResponseEntity.badRequest().body(Map.of(
"error", "Account awaiting approval",

View File

@@ -50,6 +50,12 @@ public class GoogleAuthService {
user.setVerificationCode(null);
userRepository.save(user);
}
if (!user.isApproved() && reason != null && !reason.isEmpty()) {
user.setRegisterReason(reason);
userRepository.save(user);
}
return user;
}
User user = new User();