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