fix: whitelist mode

This commit is contained in:
tim
2025-07-15 12:36:27 +08:00
parent 5b886420c5
commit b25a25f5bc
6 changed files with 30 additions and 9 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 { try {
const res = await fetch(`${API_BASE_URL}/api/auth/google`, { const res = await fetch(`${API_BASE_URL}/api/auth/google`, {
method: 'POST', method: 'POST',
@@ -28,20 +28,24 @@ export async function googleAuthWithToken(idToken, reason, redirect) {
setToken(data.token) setToken(data.token)
await loadCurrentUser() await loadCurrentUser()
toast.success('登录成功') toast.success('登录成功')
if (redirect) redirect() if (redirect_success) redirect_success()
} else if (data.reason_code === 'NOT_APPROVED') { } else if (data.reason_code === 'NOT_APPROVED') {
toast.info('当前为注册审核模式,请填写注册理由')
sessionStorage.setItem('google_id_token', idToken)
if (redirect_not_approved) redirect_not_approved()
} else if (data.reason_code === 'IS_APPROVING') {
toast.info('您的注册理由正在审批中') toast.info('您的注册理由正在审批中')
if (redirect) redirect() if (redirect_success) redirect_success()
} }
} catch (e) { } catch (e) {
toast.error('登录失败') toast.error('登录失败')
} }
} }
export async function googleSignIn(redirect, reason) { export async function googleSignIn(redirect_success, redirect_not_approved) {
try { try {
const token = await googleGetIdToken() const token = await googleGetIdToken()
await googleAuthWithToken(token, reason, redirect) await googleAuthWithToken(token, '', redirect_success, redirect_not_approved)
} catch { } catch {
/* ignore */ /* ignore */
} }

View File

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

View File

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

View File

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

View File

@@ -97,12 +97,17 @@ public class AuthController {
Optional<User> user = googleAuthService.authenticate(req.getIdToken(), req.getReason(), registerModeService.getRegisterMode()); Optional<User> user = googleAuthService.authenticate(req.getIdToken(), req.getReason(), registerModeService.getRegisterMode());
if (user.isPresent()) { if (user.isPresent()) {
if (!user.get().isApproved()) { if (!user.get().isApproved()) {
if (req.getReason() != null && !req.getReason().isEmpty()) { if (user.get().getRegisterReason() != null && !user.get().getRegisterReason().isEmpty()) {
// do not send empty notifition (while try login) // do not send empty notifition (while try login)
for (User admin : userRepository.findByRole(com.openisle.model.Role.ADMIN)) { for (User admin : userRepository.findByRole(com.openisle.model.Role.ADMIN)) {
notificationService.createNotification(admin, NotificationType.REGISTER_REQUEST, null, null, notificationService.createNotification(admin, NotificationType.REGISTER_REQUEST, null, null,
null, user.get(), null, req.getReason()); null, user.get(), null, req.getReason());
} }
return ResponseEntity.badRequest().body(Map.of(
"error", "Account awaiting approval",
"reason_code", "IS_APPROVING"
));
} }
return ResponseEntity.badRequest().body(Map.of( return ResponseEntity.badRequest().body(Map.of(
"error", "Account awaiting approval", "error", "Account awaiting approval",

View File

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