提交 93e97256 authored 作者: Whispa's avatar Whispa

commit commit

上级 a256c10e
...@@ -24,6 +24,7 @@ import org.springframework.security.authentication.BadCredentialsException; ...@@ -24,6 +24,7 @@ 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;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -117,10 +118,11 @@ public class AuthController extends Controller { ...@@ -117,10 +118,11 @@ public class AuthController extends Controller {
userInfo.setFcm(loginRequest.getFcm()); userInfo.setFcm(loginRequest.getFcm());
} }
if( loginRequest.getVeryOldPassword() != null ){ if (loginRequest.getVeryOldPassword() != null) {
userInfo.setPassword(loginRequest.getVeryOldPassword()); userInfo.setPassword(loginRequest.getVeryOldPassword());
} }
userInfo.setLastLoginTime(new Date()); userInfo.setLastLoginTime(new Date());
userInfo.setUserType(UserTypeEnum.EMAIL.getCode()); userInfo.setUserType(UserTypeEnum.EMAIL.getCode());
userRepository.save(userInfo); userRepository.save(userInfo);
...@@ -231,114 +233,120 @@ public class AuthController extends Controller { ...@@ -231,114 +233,120 @@ public class AuthController extends Controller {
// "token":"token", // "token":"token",
// } // }
// Check if firebase token is valid // Check if firebase token is valid
boolean isTokenValid = user.getToken() != null && validateFirebaseToken(user.getToken()); boolean isTokenValid = user.getToken() != null && validateFirebaseToken(user.getToken());
boolean gotFromOld = false; boolean gotFromOld = false;
String firebaseUid = user.getFirebaseUid(); String firebaseUid = user.getFirebaseUid();
// if valid do sign in if firebase Uid exist in database or register as new user // if valid do sign in if firebase Uid exist in database or register as new user
if (isTokenValid) { if (isTokenValid) {
//Query to find user from database by firebase uid //Query to find user from database by firebase uid
Optional<TbCfUserInfo> optional = userRepository.findByFirebaseUid(firebaseUid); Optional<TbCfUserInfo> optional = userRepository.findByFirebaseUid(firebaseUid);
if (!optional.isPresent() && user.getEmail() != null && !user.getEmail().isEmpty() && userRepository.existsByAccount(user.getEmail())) { if (!optional.isPresent() && user.getEmail() != null && !user.getEmail().isEmpty() && userRepository.existsByAccount(user.getEmail())) {
optional = userRepository.findFirstByAccount(user.getEmail()); optional = userRepository.findFirstByAccount(user.getEmail());
gotFromOld = optional.isPresent(); }
}
if (!optional.isPresent() && user.getPhone() != null && !user.getPhone().isEmpty() && userRepository.existsByAccount(user.getPhone().replace("+", ""))) { if (!optional.isPresent() && user.getPhone() != null && !user.getPhone().isEmpty() && userRepository.existsByAccount(user.getPhone().replace("+", ""))) {
optional = userRepository.findFirstByAccount(user.getPhone().replace("+", "")); optional = userRepository.findFirstByAccount(user.getPhone().replace("+", ""));
gotFromOld = optional.isPresent(); }
}
if (!optional.isPresent()) { if (!optional.isPresent()) {
String userid = IdUtil.createIdbyUUID(); String userid = IdUtil.createIdbyUUID();
user.setPassword(encoder.encode(firebaseUid)); user.setPassword(encoder.encode(firebaseUid));
user.setUserId(userid); user.setUserId(userid);
fillUserNecessayInfo(user); fillUserNecessayInfo(user);
user = userRepository.save(user); user = userRepository.save(user);
//赠送用户优惠券 //赠送用户优惠券
fixCoupon(user); fixCoupon(user);
} else { } else {
TbCfUserInfo userInfo = optional.get(); TbCfUserInfo userInfo = optional.get();
if (user.hasFcm()) { if (user.hasFcm()) {
userInfo.setFcm(user.getFcm()); userInfo.setFcm(user.getFcm());
} }
if (userInfo.getFirebaseUid() == null) { if (userInfo.getFirebaseUid() == null) {
userInfo.setFirebaseUid(firebaseUid); userInfo.setFirebaseUid(firebaseUid);
} }
String property = domainProperties.getProperty("user.avatar"); String property = domainProperties.getProperty("user.avatar");
property = property == null ? "" : property; property = property == null ? "" : property;
boolean b = property.equals(userInfo.getAvatar()) || userInfo.getAvatar() == null || userInfo.getAvatar().isEmpty(); boolean b = property.equals(userInfo.getAvatar()) || userInfo.getAvatar() == null || userInfo.getAvatar().isEmpty();
if (user.getAvatar() != null && b) { if (user.getAvatar() != null && b) {
userInfo.setAvatar(user.getAvatar()); userInfo.setAvatar(user.getAvatar());
} }
boolean b1 = userInfo.getNick() == null || userInfo.getNick().isEmpty(); boolean b1 = userInfo.getNick() == null || userInfo.getNick().isEmpty();
if (user.getNick() != null && b1) { if (user.getNick() != null && b1) {
userInfo.setNick(user.getNick()); userInfo.setNick(user.getNick());
} }
if (user.getEmail() != null) { if (user.getEmail() != null) {
userInfo.setEmail(user.getEmail()); userInfo.setEmail(user.getEmail());
} }
if (user.getUserType() != null) { if (user.getUserType() != null) {
userInfo.setUserType(user.getUserType()); userInfo.setUserType(user.getUserType());
} }
if (user.getPhone() != null) { if (user.getPhone() != null) {
userInfo.setPhone(user.getPhone()); userInfo.setPhone(user.getPhone());
} }
user = userInfo; user = userInfo;
} }
// // generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if
// // user exist we have to generate token also
// if (user.getPassword() == null || gotFromOld) {
// user.setFirebaseUid(firebaseUid);// Assign user from database to the user we have to return back to request
// // user.setPassword(encoder.encode(firebaseUid));// Assign user from database to the user we have to return back to request
// }
String very = gotFromOld ? user.getPassword() : null;
// generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if
// user exist we have to generate token also
if (user.getPassword() == null || gotFromOld) {
user.setFirebaseUid(firebaseUid);// Assign user from database to the user we have to return back to request
user.setPassword(encoder.encode(firebaseUid));// Assign user from database to the user we have to return back to request
}
fixCode(user);
fixCode(user); //addToNetwork(user);
//addToNetwork(user); user.setLastLoginTime(new Date());
userRepository.save(user);
//注册成功 创建token
user.setLastLoginTime(new Date());
userRepository.save(user);
//注册成功 创建token
LoginRequest loginRequest = new LoginRequest(firebaseUid, firebaseUid); Authentication authentication;
try {
authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(firebaseUid, null, AuthorityUtils.createAuthorityList("ROLE_USER")));
} catch (BadCredentialsException e) {
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Invalid username or password");
}
loginRequest.setVeryOldPassword(very); SecurityContextHolder.getContext().setAuthentication(authentication);
String jwt = jwtUtils.generateJwtToken(authentication);
return authenticateUser(loginRequest); user.setToken(jwt);
return new Result<>(user);
} else {
return new Result<>(ResultCodeEnum.ILLEGAL_ARGUMENT.getCode(), ResultCodeEnum.ILLEGAL_ARGUMENT.getDesc()); } else {
} return new Result<>(ResultCodeEnum.ILLEGAL_ARGUMENT.getCode(), ResultCodeEnum.ILLEGAL_ARGUMENT.getDesc());
}
} }
...@@ -373,7 +381,7 @@ public class AuthController extends Controller { ...@@ -373,7 +381,7 @@ public class AuthController extends Controller {
private void fixCode(TbCfUserInfo user) { private void fixCode(TbCfUserInfo user) {
if( user.getCode() == null ) { if (user.getCode() == null) {
try { try {
Query nativeQuery = entityManager.createNativeQuery("select b.code as data FROM user_info b where b.external_id=:user limit 1"); Query nativeQuery = entityManager.createNativeQuery("select b.code as data FROM user_info b where b.external_id=:user limit 1");
...@@ -382,7 +390,7 @@ public class AuthController extends Controller { ...@@ -382,7 +390,7 @@ public class AuthController extends Controller {
if (nativeQuery.getSingleResult() != null) { if (nativeQuery.getSingleResult() != null) {
user.setCode(nativeQuery.getSingleResult().toString()); user.setCode(nativeQuery.getSingleResult().toString());
} }
}catch (Exception e){ } catch (Exception e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} }
......
...@@ -7,6 +7,7 @@ public class LoginRequest { ...@@ -7,6 +7,7 @@ public class LoginRequest {
private String password; private String password;
private String veryOldPassword; private String veryOldPassword;
private String fcm; private String fcm;
private boolean fromUser = false;
public LoginRequest(){ public LoginRequest(){
...@@ -18,7 +19,15 @@ public class LoginRequest { ...@@ -18,7 +19,15 @@ public class LoginRequest {
this.password = password; this.password = password;
} }
public LoginRequest(String account,String password,String fcm){ public void setFromUser(boolean fromUser) {
this.fromUser = fromUser;
}
public boolean isFromUser() {
return fromUser;
}
public LoginRequest(String account, String password, String fcm){
this.account = account; this.account = account;
this.password = password; this.password = password;
this.fcm = fcm; this.fcm = fcm;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论