Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
[chore] Sign Up With Apple Improvements (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedbashir committed Jul 7, 2022
1 parent ac76f67 commit 2563259
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Source/AppleSocial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
//

import AuthenticationServices
import KeychainSwift

private let errorDomain = "AppleSocial"
private let appleNameKey = "APPLE_SIGNUP_NAME_KEY"


typealias AppleLoginCompletionHandler = (OEXRegisteringUserDetails?, _ accessToken: String?, Error?) -> Void


class AppleSocial: NSObject {
@objc class AppleSocial: NSObject {

static let shared = AppleSocial()
@objc static let shared = AppleSocial()
private var completionHandler: AppleLoginCompletionHandler?

private override init() {
Expand All @@ -35,6 +38,10 @@ class AppleSocial: NSObject {
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}

@objc func savedUserName() -> String {
return KeychainSwift().get(appleNameKey) ?? ""
}
}

extension AppleSocial: ASAuthorizationControllerDelegate {
Expand All @@ -50,12 +57,23 @@ extension AppleSocial: ASAuthorizationControllerDelegate {
return
}

let firstName = credentials.fullName?.givenName
let lastName = credentials.fullName?.familyName
let firstName = credentials.fullName?.givenName ?? ""
let lastName = credentials.fullName?.familyName ?? ""
let email = credentials.email ?? ""

var name = "\(firstName) \(lastName)"
if !name.isEmpty && name != " " {
// Save data in keychain because apple won't return data on 2nd request
// if sign up was not completed
KeychainSwift().set(name, forKey: appleNameKey)
}

if firstName.isEmpty && lastName.isEmpty {
name = savedUserName()
}

let userDetails = OEXRegisteringUserDetails()
userDetails.name = "\(firstName ?? "") \(lastName ?? "")"
userDetails.name = name
userDetails.email = email

if let data = credentials.identityToken, let code = String(data: data, encoding: .utf8) {
Expand Down Expand Up @@ -98,7 +116,6 @@ extension AppleSocial: ASAuthorizationControllerDelegate {
let segments = jwt.components(separatedBy: ".")
return try decodeJWTPart(segments[1])
}

}

extension AppleSocial: ASAuthorizationControllerPresentationContextProviding {
Expand Down
3 changes: 3 additions & 0 deletions Source/OEXRegistrationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ - (void)populateDefaultFormFields {
self.email = fieldController.field.defaultValue;
}
}
else if ([[fieldController field].name isEqualToString:@"name"] && [_externalProvider.displayName isEqualToString:@"Apple"]) {
[fieldController setValue:[[AppleSocial shared] savedUserName]];
}
}
}

Expand Down

0 comments on commit 2563259

Please sign in to comment.