Last active
November 26, 2019 16:41
-
-
Save styrken/10cd0700a894edd693b724d0a4166d2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppleSignInViewController.swift | |
// | |
// Created by Rasmus Styrk on 26/11/2019. | |
// Copyright © 2019 House of Code ApS. All rights reserved. | |
// | |
import UIKit | |
import AuthenticationServices | |
class AppleSignInViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// TODO: Add button to call self.inititateAppleLogin() | |
} | |
func inititateAppleLogin() { | |
if #available(iOS 13.0, *) { | |
let appleIDProvider = ASAuthorizationAppleIDProvider() | |
let request = appleIDProvider.createRequest() | |
request.requestedScopes = [.fullName, .email] | |
let authorizationController = ASAuthorizationController(authorizationRequests: [request]) | |
authorizationController.delegate = self | |
authorizationController.presentationContextProvider = self | |
authorizationController.performRequests() | |
} else { | |
// TODO: Show error? | |
} | |
} | |
func validateWithServer(name: String, userIdentity: String, jwt: String) { | |
// TODO: Send off userIdeneity, name and jwtToken to server for validation | |
print("User: \(userIdentity), with name: \(name) and jwt token: \(jwt)") | |
} | |
} | |
@available(iOS 13.0, *) | |
extension AppleSignInViewController: ASAuthorizationControllerDelegate { | |
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { | |
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential { | |
let userIdentity = appleIDCredential.user | |
var name: String = "No Name" | |
if let fullName = appleIDCredential.fullName { | |
var nameComponents: [String] = [] | |
if let givenName = fullName.givenName { | |
nameComponents.append(givenName) | |
} | |
if let familyName = fullName.familyName { | |
nameComponents.append(familyName) | |
} | |
if nameComponents.count > 0 { | |
name = nameComponents.joined(separator: " ") | |
} | |
} | |
if let jwtData = appleIDCredential.identityToken, | |
let jwtToken = String(data: jwtData, encoding: .utf8) { | |
self.validateWithServer(name: name, userIdentity: userIdentity, jwt: jwtToken) | |
} else { | |
// Show error? Missing jwt token | |
} | |
} | |
} | |
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { | |
// Show error | |
} | |
} | |
@available(iOS 13.0, *) | |
extension AppleSignInViewController: ASAuthorizationControllerPresentationContextProviding { | |
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { | |
return self.view.window! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment