提交 aa453402 authored 作者: Whispa's avatar Whispa

commit

上级 028acb5a
...@@ -22,6 +22,7 @@ import com.example.afrishop_v3.util.IdUtil; ...@@ -22,6 +22,7 @@ import com.example.afrishop_v3.util.IdUtil;
import org.hashids.Hashids; import org.hashids.Hashids;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
...@@ -72,9 +73,13 @@ public class AuthController extends Controller { ...@@ -72,9 +73,13 @@ public class AuthController extends Controller {
return new Result<>(ResultCodeEnum.VALIDATE_ERROR.getCode(),"User not found"); return new Result<>(ResultCodeEnum.VALIDATE_ERROR.getCode(),"User not found");
} }
Authentication authentication;
Authentication authentication = authenticationManager.authenticate( try {
new UsernamePasswordAuthenticationToken(loginRequest.getAccount(), loginRequest.getPassword())); authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequest.getAccount(), loginRequest.getPassword()));
}catch (BadCredentialsException e){
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Invalid username or password");
}
SecurityContextHolder.getContext().setAuthentication(authentication); SecurityContextHolder.getContext().setAuthentication(authentication);
String jwt = jwtUtils.generateJwtToken(authentication); String jwt = jwtUtils.generateJwtToken(authentication);
...@@ -108,15 +113,19 @@ public class AuthController extends Controller { ...@@ -108,15 +113,19 @@ public class AuthController extends Controller {
// return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Error: Username is already taken!"); // return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Error: Username is already taken!");
// } // }
boolean byEmail = userRepository.existsByFirebaseUid(signUpRequest.getEmail()); String email = signUpRequest.getEmail();
email = email == null ? "" : email.trim();
boolean byEmail = userRepository.existsByFirebaseUid(email);
if ( byEmail ) { if ( byEmail ) {
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Error: Email is already in use!"); return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Error: Email is already in use!");
} }
signUpRequest.setFirebaseUid(signUpRequest.getEmail()); signUpRequest.setFirebaseUid(email);
signUpRequest.setAccount(signUpRequest.getEmail()); signUpRequest.setAccount(email);
signUpRequest.setUserId(IdUtil.createIdbyUUID()); signUpRequest.setUserId(IdUtil.createIdbyUUID());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论