Skip to content

Commit

Permalink
Merge pull request #126 from Kek-i/feature/31-socialLogin
Browse files Browse the repository at this point in the history
fix: ์• ํ”Œ๋กœ๊ทธ์ธ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ
  • Loading branch information
chayoosang authored Mar 29, 2023
2 parents 72d8da6 + 30c8bee commit 147d7f6
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions KeKi/Scenes/Login/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ extension LoginViewController: ASAuthorizationControllerDelegate, ASAuthorizatio
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential else { return }

if let email = UserDefaults.standard.value(forKey: "appleEmail") {
requestSocialLogin(email: email as! String, provider: "์• ํ”Œ")

} else if let email = appleIDCredential.email {
UserDefaults.standard.set(email, forKey: "appleEmail")
if let email = appleIDCredential.email { // 2๋ฒˆ์งธ ์• ํ”Œ ๋กœ๊ทธ์ธ๋ถ€ํ„ฐ๋Š” email์ด identityToken์— ๋“ค์–ด์žˆ์Œ.
requestSocialLogin(email: email, provider: "์• ํ”Œ")
} else {
if let tokenString = String(data: appleIDCredential.identityToken ?? Data(), encoding: .utf8) {
let email = decode(jwtToken: tokenString)["email"] as? String ?? ""
requestSocialLogin(email: email, provider: "์• ํ”Œ")
}
}

}

func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
Expand All @@ -175,6 +175,36 @@ extension LoginViewController: ASAuthorizationControllerDelegate, ASAuthorizatio
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}

// (๋‘๋ฒˆ์งธ ์ดํ›„๋ถ€ํ„ฐ์˜ ์• ํ”Œ๋กœ๊ทธ์ธ์„ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ) JWTToken -> dictionary
private func decode(jwtToken jwt: String) -> [String: Any] {

func base64UrlDecode(_ value: String) -> Data? {
var base64 = value
.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")

let length = Double(base64.lengthOfBytes(using: String.Encoding.utf8))
let requiredLength = 4 * ceil(length / 4.0)
let paddingLength = requiredLength - length
if paddingLength > 0 {
let padding = "".padding(toLength: Int(paddingLength), withPad: "=", startingAt: 0)
base64 = base64 + padding
}
return Data(base64Encoded: base64, options: .ignoreUnknownCharacters)
}

func decodeJWTPart(_ value: String) -> [String: Any]? {
guard let bodyData = base64UrlDecode(value),
let json = try? JSONSerialization.jsonObject(with: bodyData, options: []), let payload = json as? [String: Any] else {
return nil
}
return payload
}

let segments = jwt.components(separatedBy: ".")
return decodeJWTPart(segments[1]) ?? [:]
}
}


Expand Down

0 comments on commit 147d7f6

Please sign in to comment.