TASK:谷歌登录

This commit is contained in:
shahaibo
2024-11-12 15:34:43 +08:00
parent 74e6d5a1da
commit fadb5faf0d
3 changed files with 38 additions and 18 deletions

View File

@@ -4,7 +4,11 @@ import lombok.Data;
@Data
public class GoogleUser {
private String id;
private String sub; // 用户唯一标识
private String email;
private String name;
private String picture;
private boolean emailVerified;
private String givenName;
private String familyName;
}

View File

@@ -6,5 +6,8 @@ import lombok.Data;
public class GoogleTokenResponse {
private String accessToken;
private String idToken;
private long expiresIn;
private String tokenType;
private String scope;
}

View File

@@ -1857,27 +1857,39 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
@Override
public String googleCallback(String code, HttpSession session) {
try {
log.info("code:" + code);
log.info("code: " + code);
// 使用 code 获取 Google 用户信息
GoogleUser googleUser = getGoogleUserFromCode(code);
log.info("googleUser:" + JSON.toJSONString(googleUser));
log.info("googleUser: " + JSON.toJSONString(googleUser));
// 检查数据库中是否已有该用户
// QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
// qw.lambda().eq(AccountExtend::getAuth, googleUser.getId());
// List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
// Account existingUser = findUserByGoogleId(googleUser.getId());
return "Login successful";
// if (CollectionUtil.isNotEmpty(accountExtends)) {
// // 用户已存在,直接登录
//// session.setAttribute("user", existingUser);
// return "Login successful";
// } else {
// // 用户不存在,创建新用户(自动注册)
//// User newUser = googleAuthService.registerNewUser(googleUser);
//// session.setAttribute("user", newUser);
// return "Registration and login successful";
// }
QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(Account::getUserEmail, googleUser.getEmail()); // 根据邮箱查询用户
List<Account> accounts = accountMapper.selectList(queryWrapper);
if (CollectionUtil.isNotEmpty(accounts)) {
// 用户已存在,直接登录
session.setAttribute("user", accounts.get(0));
return "Login successful";
} else {
// 用户不存在,创建新用户(自动注册)
Account newUser = new Account();
newUser.setUserEmail(googleUser.getEmail());
newUser.setUserName(googleUser.getName());
newUser.setUserPassword("Third-000000");
newUser.setLanguage(Language.ENGLISH.name());
newUser.setValidStartTime(System.currentTimeMillis());
newUser.setValidEndTime(toDayEnd(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli()));
newUser.setCreateDate(new Date());
newUser.setIsTrial(1);
newUser.setIsBeginner(1);
newUser.setCredits(BigDecimal.valueOf(100));
newUser.setSystemUser(3);
accountMapper.insert(newUser);
session.setAttribute("user", newUser);
return "Registration and login successful";
}
} catch (Exception e) {
return "Error processing Google login: " + e.getMessage();
}
@@ -1887,6 +1899,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v3/userinfo";
private static final String CLIENT_ID = "194770296147-njd68pm7tnapgonkj2h48mhf63n15n3f.apps.googleusercontent.com";
private static final String CLIENT_SECRET = "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";
// private static final String REDIRECT_URI = "https://0551-117-143-125-51.ngrok-free.app/api/third/party/auth/google_callback";
private static final String REDIRECT_URI = "https://develop.api.aida.com.hk/api/third/party/auth/google_callback";
public GoogleUser getGoogleUserFromCode(String code) {