-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// SignInWithAppleView.swift | ||
// Examples | ||
// | ||
// Created by Guilherme Souza on 07/07/23. | ||
// | ||
|
||
import SwiftUI | ||
import AuthenticationServices | ||
import CryptoKit | ||
|
||
struct SignInWithAppleView: View { | ||
@Environment(\.goTrueClient) private var client | ||
|
||
@State var nonce: String? | ||
|
||
var body: some View { | ||
SignInWithAppleButton { request in | ||
self.nonce = sha256(randomNonceString()) | ||
request.requestedScopes = [.fullName, .email] | ||
request.nonce = nonce | ||
} onCompletion: { result in | ||
switch result { | ||
case .success(let authorization): | ||
guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential else { | ||
return | ||
} | ||
|
||
signInWithApple(credential: credential) | ||
|
||
case .failure(let error): | ||
dump(error) | ||
} | ||
} | ||
.fixedSize() | ||
} | ||
|
||
private func signInWithApple(credential: ASAuthorizationAppleIDCredential) { | ||
Task { | ||
do { | ||
try await client.signInWithApple(credential: credential, nonce: nonce) | ||
} catch { | ||
dump(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func randomNonceString(length: Int = 32) -> String { | ||
precondition(length > 0) | ||
var randomBytes = [UInt8](repeating: 0, count: length) | ||
let errorCode = SecRandomCopyBytes(kSecRandomDefault, randomBytes.count, &randomBytes) | ||
if errorCode != errSecSuccess { | ||
fatalError( | ||
"Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)" | ||
) | ||
} | ||
|
||
let charset: [Character] = | ||
Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._") | ||
|
||
let nonce = randomBytes.map { byte in | ||
// Pick a random character from the set, wrapping around if needed. | ||
charset[Int(byte) % charset.count] | ||
} | ||
|
||
return String(nonce) | ||
} | ||
|
||
@available(iOS 13, *) | ||
private func sha256(_ input: String) -> String { | ||
let inputData = Data(input.utf8) | ||
let hashedData = SHA256.hash(data: inputData) | ||
let hashString = hashedData.compactMap { | ||
String(format: "%02x", $0) | ||
}.joined() | ||
|
||
return hashString | ||
} | ||
|
||
|
||
#Preview { | ||
SignInWithAppleView() | ||
} |
This file contains 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