Skip to content

Commit

Permalink
[Add] #16 소셜 로그인정보 Firebase Auth와 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
KakaoTocs committed Sep 4, 2021
1 parent e7107f9 commit b12bd18
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion BMDJ/ViewController/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import RxCocoa
import SnapKit

import GoogleSignIn
import FirebaseAuth

final class LoginViewController: UIViewController, View {

Expand Down Expand Up @@ -208,10 +209,13 @@ final class LoginViewController: UIViewController, View {
GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
guard error == nil else { return }
print("Google User ID: \(user?.userID ?? "nil")")
if let idToken = user?.authentication.idToken {
if let idToken = user?.authentication.idToken,
let accessToken = user?.authentication.accessToken {
AuthClient.shared.google(token: idToken)
.subscribe(onNext: { token in
print("Google: \(token)")
let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
Auth.auth().signIn(with: credential)
self.loginSuccess(token: token)
})
.disposed(by: self.disposeBag)
Expand Down Expand Up @@ -271,6 +275,9 @@ extension LoginViewController: ASAuthorizationControllerDelegate {
AuthClient.shared.apple(userID: token)
.subscribe(onNext: { id in
print("Apple: \(id)")
let nonce = self.randomNonceString()
let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: token, rawNonce: nonce)
Auth.auth().signIn(with: credential)
self.loginSuccess(token: id)
})
.disposed(by: disposeBag)
Expand All @@ -282,4 +289,36 @@ extension LoginViewController: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
print("Apple Login Fail")
}

private func randomNonceString(length: Int = 32) -> String {
precondition(length > 0)
let charset: Array<Character> =
Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
var result = ""
var remainingLength = length

while remainingLength > 0 {
let randoms: [UInt8] = (0 ..< 16).map { _ in
var random: UInt8 = 0
let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
if errorCode != errSecSuccess {
fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)")
}
return random
}

randoms.forEach { random in
if remainingLength == 0 {
return
}

if random < charset.count {
result.append(charset[Int(random)])
remainingLength -= 1
}
}
}

return result
}
}

0 comments on commit b12bd18

Please sign in to comment.