Last active
June 28, 2023 02:37
-
-
Save hulk510/7f13636d79d823adba609304b61bd040 to your computer and use it in GitHub Desktop.
SwiftUI SignInWithApple
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
import SwiftUI | |
import Firebase | |
import FirebaseAuth | |
class AuthState: ObservableObject { | |
func signIn(credential: OAuthCredential) async throws { | |
_ = try await auth.signIn(with: credential) | |
} | |
func deleteUser(code: String) async throws { | |
try await auth.revokeToken(withAuthorizationCode: code) | |
... | |
} | |
} |
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
import Foundation | |
import CryptoKit | |
struct CryptoUtils { | |
enum CryptoError: Error { | |
case nonceGenerationFailed | |
} | |
static 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 | |
} | |
static func randomNonceString(length: Int = 32) throws -> String { | |
precondition(length > 0) | |
let charset: [Character] = | |
Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._") | |
var result = "" | |
var remainingLength = length | |
while remainingLength > 0 { | |
let randoms: [UInt8] = try (0 ..< 16).map { _ in | |
var random: UInt8 = 0 | |
let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random) | |
if errorCode != errSecSuccess { | |
throw CryptoError.nonceGenerationFailed | |
} | |
return random | |
} | |
randoms.forEach { random in | |
if remainingLength == 0 { | |
return | |
} | |
if random < charset.count { | |
result.append(charset[Int(random)]) | |
remainingLength -= 1 | |
} | |
} | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment