애플은 2019년 애플 로그인 기능을 발표했습니다. 동시에 앱 내에 다른 소셜 로그인 서비스를 사용하고 있다면 반드시! 애플 로그인을 제공해야 한다는 심사지침도 함께 내놓았습니다.
일단 클라이언트가 해야될 작업은 크게 세가지이다.
Apple ID의 인증 결과 및 요청 값인 email과 fullName을 전달받기 위해 채택합니다.
→ ASAuthorizationControllerDelegate
인증 성공 → didCompleteWithAuthorization
인증을 성공하면 알 수 있는 것
: userIdentifier
, fullName
, email
, authorizationCode
, identityToken
이 중에서 우리는 identityToken을 서버에 보내서 유효한 jwt Token을 받을 수 있다!
인증 실패 → didCompleteWithError
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
switch authorization.credential {
/// Apple ID
case let appleIDCredential as ASAuthorizationAppleIDCredential:
/// 계정 정보 가져오기
let userIdentifier = appleIDCredential.user
let fullName = appleIDCredential.fullName
let email = appleIDCredential.email
if let authorizationCode = appleIDCredential.authorizationCode,
let identityToken = appleIDCredential.identityToken,
let authString = String(data: authorizationCode, encoding: .utf8),
let tokenString = String(data: identityToken, encoding: .utf8) {
print("authorizationCode: \\(authorizationCode)")
print("identityToken: \\(identityToken)")
print("authString: \\(authString)")
print("tokenString: \\(tokenString)")
}
print("User ID : \\(userIdentifier)")
print("User Email : \\(email ?? "")")
print("User Name : \\((fullName?.givenName ?? "") + (fullName?.familyName ?? ""))")
default:
break
}
}
// Apple ID 연동 실패 시
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
// Handle error.
}
}