feat: sign in with apple 待后端接口

This commit is contained in:
jinyaqiu 2025-08-18 17:20:13 +08:00
parent ace3e3dd14
commit 35e949cf17
2 changed files with 24 additions and 7 deletions

View File

@ -129,17 +129,22 @@ struct LoginView: View {
///
private func handleAppleSignIn(result: Result<ASAuthorization, Error>) {
print("🔵 [Apple Sign In] 开始处理登录结果...")
switch result {
case .success(let authResults):
print("✅ [Apple Sign In] 登录授权成功")
processAppleIDCredential(authResults.credential)
case .failure(let error):
print("❌ [Apple Sign In] 登录失败: \(error.localizedDescription)")
handleSignInError(error)
}
}
/// ID
private func processAppleIDCredential(_ credential: ASAuthorizationCredential) {
print("🔵 [Apple ID] 开始处理凭证...")
guard let appleIDCredential = credential as? ASAuthorizationAppleIDCredential else {
print("❌ [Apple ID] 凭证类型不匹配")
showError(message: "无法处理Apple ID凭证")
return
}
@ -154,9 +159,12 @@ struct LoginView: View {
.compactMap { $0 }
.joined(separator: " ")
print(" [Apple ID] 用户数据 - ID: \(userId), 邮箱: \(email.isEmpty ? "未提供" : email), 姓名: \(fullName.isEmpty ? "未提供" : fullName)")
//
guard let identityTokenData = appleIDCredential.identityToken,
let identityToken = String(data: identityTokenData, encoding: .utf8) else {
print("❌ [Apple ID] 无法获取身份令牌")
showError(message: "无法获取身份令牌")
return
}
@ -165,9 +173,12 @@ struct LoginView: View {
var authCode: String? = nil
if let authCodeData = appleIDCredential.authorizationCode {
authCode = String(data: authCodeData, encoding: .utf8)
print(" [Apple ID] 获取到授权码")
} else {
print(" [Apple ID] 未获取到授权码(可选)")
}
//
print("🔵 [Apple ID] 准备调用后端认证...")
authenticateWithBackend(
userId: userId,
email: email,
@ -188,8 +199,8 @@ struct LoginView: View {
authCode: String?
) {
isLoading = true
print("🔵 [Backend] 开始后端认证...")
// TODO: API
let url = "https://your-api-endpoint.com/api/auth/apple"
var parameters: [String: Any] = [
"appleUserId": userId,
@ -203,7 +214,8 @@ struct LoginView: View {
parameters["authorizationCode"] = authCode
}
//
print("📤 [Backend] 请求参数: \(parameters)")
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.validate()
.responseJSON { response in
@ -211,9 +223,13 @@ struct LoginView: View {
switch response.result {
case .success(let value):
print("认证成功: \(value)")
print("✅ [Backend] 认证成功: \(value)")
self.handleSuccessfulAuthentication()
case .failure(let error):
print("❌ [Backend] 认证失败: \(error.localizedDescription)")
if let data = response.data, let json = String(data: data, encoding: .utf8) {
print("❌ [Backend] 错误详情: \(json)")
}
self.handleAuthenticationError(error)
}
}
@ -223,22 +239,23 @@ struct LoginView: View {
///
private func handleSuccessfulAuthentication() {
print("✅ [Auth] 登录成功,准备关闭登录页面...")
DispatchQueue.main.async {
self.dismiss() //
self.dismiss()
}
}
///
private func handleSignInError(_ error: Error) {
let errorMessage = (error as NSError).localizedDescription
print("苹果登录失败: \(errorMessage)")
print("❌ [Auth] 登录错误: \(errorMessage)")
showError(message: "登录失败: \(error.localizedDescription)")
}
///
private func handleAuthenticationError(_ error: AFError) {
let errorMessage = error.localizedDescription
print("API错误: \(errorMessage)")
print("❌ [Auth] 认证错误: \(errorMessage)")
showError(message: "登录失败: \(errorMessage)")
}