SSUMC-6th / iOS_A

SSUMC 6๊ธฐ iOS ์Šคํ„ฐ๋”” A์กฐ
0 stars 0 forks source link

[JC/Week4] Token & UserDefaults #38

Closed jayn2u closed 6 months ago

jayn2u commented 6 months ago

๐Ÿ“Summary

ํ•ด๋‹น PR์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”.

๊ตฌ๊ธ€ ์‚ฌ์šฉ์ž ์ธ์ฆ ์‚ฌ์šฉ ๊ฐ„ ์ž๋™๋กœ๊ทธ์ธ ๋กœ์ง์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’กPR Point

์ฝ”๋“œ๋“ค์—์„œ ํ•ต์‹ฌ ์ฝ”๋“œ๋“ค์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๋˜๋Š” ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ ํ•ด์ฃผ์„ธ์š”.

// SceneDelegate.swift

if let currentUser = Auth.auth().currentUser {
      if let savedUID = UserDefaults.standard.string(forKey: "currentUserUID"), savedUID == currentUser.uid {
        window?.rootViewController = GoogleAuthLoginedViewController()
      } else {
        window?.rootViewController = GoogleAuthViewController()
      }
    } else {
      window?.rootViewController = GoogleAuthViewController()
    }

๐Ÿ”‘ย Simulator

simulator ์˜์ƒ์„ ํ™”๋ฉด ๋…นํ™” ํ›„ ๋ณด์—ฌ์ฃผ์„ธ์š”.

Simulator Screen Recording - iPhone 15 Pro - 2024-05-12 at 14 54 25

jayn2u commented 6 months ago

3f6d7c ์ปค๋ฐ‹ ์ œ๊ฑฐ ์„ค๋ช…

์„ค๋ช…

jayn2u commented 6 months ago

๐Ÿ“Summary

ํ•ด๋‹น PR์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”.

๐Ÿ’กPR Point

์ฝ”๋“œ๋“ค์—์„œ ํ•ต์‹ฌ ์ฝ”๋“œ๋“ค์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๋˜๋Š” ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ ํ•ด์ฃผ์„ธ์š”.

@objc func loginButtonTapped() {
    guard let inputEmailString = emailTextField.text,
      let inputPasswordString =
        passwordTextField.text
    else { return }

    Auth.auth().signIn(withEmail: inputEmailString, password: inputPasswordString) { authResult, error in
      if let error = error {
        debugPrint("Error occured in signIn process")
        debugPrint("Error message below here")
        debugPrint(error.localizedDescription)
      }

      guard let authResult = authResult else { return }
      debugPrint(authResult)

      let profileLoginedViewController = ProfileLoginedViewController()
      profileLoginedViewController.emailLabel.text = inputEmailString

      guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else { return }
      sceneDelegate.window?.rootViewController = profileLoginedViewController
    }

  }

@objc func logoutButtonTapped() {
    debugPrint("Logout success...!")

    guard let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate else { return }
    sceneDelegate.window?.rootViewController = MainViewController()
  }

๐Ÿค” Question

์ž‘์—…ํ•˜์‹œ๋ฉด์„œ ๊ถ๊ธˆํ–ˆ๋˜ ์งˆ๋ฌธ๋“ค์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.

๐Ÿ”‘ย Simulator

simulator ์˜์ƒ์„ ํ™”๋ฉด ๋…นํ™” ํ›„ ๋ณด์—ฌ์ฃผ์„ธ์š”.

Simulator Screen Recording - iPhone 15 Pro - 2024-05-11 at 20 20 30

jayn2u commented 6 months ago

๐Ÿ“Summary

ํ•ด๋‹น PR์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”.

Firebase์™€ UserDefaults๋ฅผ ์‚ฌ์šฉํ•ด์„œ Auto-login ๋กœ์ง ๊ตฌํ˜„์„ ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’กPR Point

์ฝ”๋“œ๋“ค์—์„œ ํ•ต์‹ฌ ์ฝ”๋“œ๋“ค์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๋˜๋Š” ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ ํ•ด์ฃผ์„ธ์š”.

๐Ÿค” Question

์ž‘์—…ํ•˜์‹œ๋ฉด์„œ ๊ถ๊ธˆํ–ˆ๋˜ ์งˆ๋ฌธ๋“ค์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.

๐Ÿ”‘ย Simulator

simulator ์˜์ƒ์„ ํ™”๋ฉด ๋…นํ™” ํ›„ ๋ณด์—ฌ์ฃผ์„ธ์š”.

Simulator Screen Recording - iPhone 15 Pro - 2024-05-12 at 08 53 30

jayn2u commented 6 months ago

๐Ÿ“Summary

ํ•ด๋‹น PR์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”.

๐Ÿ’กPR Point

์ฝ”๋“œ๋“ค์—์„œ ํ•ต์‹ฌ ์ฝ”๋“œ๋“ค์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๋˜๋Š” ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ ํ•ด์ฃผ์„ธ์š”.

PR Point - Code

  @objc private func makeUserAuthenticationInfo() {
    guard let clientID = FirebaseApp.app()?.options.clientID else { return }

    // Create Google Sign In configuration object.
    let config = GIDConfiguration(clientID: clientID)
    GIDSignIn.sharedInstance.configuration = config

    // Start the sign in flow!
    GIDSignIn.sharedInstance.signIn(withPresenting: self) { [unowned self] result, error in
      guard error == nil else {
        return
      }

      guard let user = result?.user,
        let idToken = user.idToken?.tokenString
      else {
        return
      }

      let credential = GoogleAuthProvider.credential(
        withIDToken: idToken,
        accessToken: user.accessToken.tokenString)

      Auth.auth().signIn(with: credential) { authResult, error in
        if let error = error {
          debugPrint(error.localizedDescription)
        }

        guard let authResult = authResult else { return }
        debugPrint(authResult)
      }

    }

  }

๐Ÿค” Question

์ž‘์—…ํ•˜์‹œ๋ฉด์„œ ๊ถ๊ธˆํ–ˆ๋˜ ์งˆ๋ฌธ๋“ค์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.

๐Ÿ”‘ย Simulator

simulator ์˜์ƒ์„ ํ™”๋ฉด ๋…นํ™” ํ›„ ๋ณด์—ฌ์ฃผ์„ธ์š”.

Simulator Screen Recording - iPhone 15 Pro - 2024-05-12 at 11 28 09

Screenshot 2024-05-12 at 11 29 19

jayn2u commented 6 months ago

๐Ÿ“Summary

ํ•ด๋‹น PR์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”.

๐Ÿ’กPR Point

์ฝ”๋“œ๋“ค์—์„œ ํ•ต์‹ฌ ์ฝ”๋“œ๋“ค์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๋˜๋Š” ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ ํ•ด์ฃผ์„ธ์š”.

  @objc func logoutButtonTapped() {
    let firebaseAuth = Auth.auth()

    do {
      try firebaseAuth.signOut()
      debugPrint("Log-out successfully...!")
      guard
        let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate
      else { return }
      sceneDelegate.window?.rootViewController = GoogleAuthViewController()
    } catch {
      debugPrint("Error occured in signing out...!")
    }
  }

๐Ÿค” Question

์ž‘์—…ํ•˜์‹œ๋ฉด์„œ ๊ถ๊ธˆํ–ˆ๋˜ ์งˆ๋ฌธ๋“ค์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.

๐Ÿ”‘ย Simulator

simulator ์˜์ƒ์„ ํ™”๋ฉด ๋…นํ™” ํ›„ ๋ณด์—ฌ์ฃผ์„ธ์š”.

Simulator Screen Recording - iPhone 15 Pro - 2024-05-12 at 12 40 44

jayn2u commented 6 months ago

๐Ÿ“Summary

ํ•ด๋‹น PR์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”.

๊ตฌ๊ธ€ ์‚ฌ์šฉ์ž ์ธ์ฆ ์‚ฌ์šฉ ๊ฐ„ ์ž๋™๋กœ๊ทธ์ธ ๋กœ์ง์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’กPR Point

์ฝ”๋“œ๋“ค์—์„œ ํ•ต์‹ฌ ์ฝ”๋“œ๋“ค์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๋˜๋Š” ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ ํ•ด์ฃผ์„ธ์š”.

// SceneDelegate.swift

if let currentUser = Auth.auth().currentUser {
      if let savedUID = UserDefaults.standard.string(forKey: "currentUserUID"), savedUID == currentUser.uid {
        window?.rootViewController = GoogleAuthLoginedViewController()
      } else {
        window?.rootViewController = GoogleAuthViewController()
      }
    } else {
      window?.rootViewController = GoogleAuthViewController()
    }

๐Ÿ”‘ย Simulator

simulator ์˜์ƒ์„ ํ™”๋ฉด ๋…นํ™” ํ›„ ๋ณด์—ฌ์ฃผ์„ธ์š”.

Simulator Screen Recording - iPhone 15 Pro - 2024-05-12 at 14 54 25

jayn2u commented 6 months ago

์ œ์ด์‹œ ์›Œํฌ๋ถ ๋ฏธ์…˜์— ๋”ํ•ด์„œ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ๊นŒ์ง€ ํผ ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค!!!

๋”ํ•ด์„œ ์ œ๊ฐ€ ์•„๋Š” ์ผ๋ฐ˜ ๋กœ๊ทธ์ธ ๋กœ์ง์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด๋“œ๋ฆฌ์ž๋ฉด,, ๋จผ์ € ๋กœ๊ทธ์ธ์„ ์„ฑ๊ณตํ•˜๋ฉด ์„œ๋ฒ„์—์„œ accessToken๊ณผ refreshToken์„ ์ฃผ์–ด UserDefault๊ฐ€ ์•„๋‹Œ keyChain์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ž๋™๋กœ๊ทธ์ธ ์•Œ๋ฆผ์„ ๋„์›Œ์„œ UserDefault์—๋Š” isAutoLogin์ด๋ผ๋Š” ์ž๋™๋กœ๊ทธ์ธ ๋ณ€์ˆ˜๋งŒ ๋‹ด์•„๋†”์š”! (์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋“ฑ ํ”„๋กœํ•„ ์ •๋ณด ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์–ด์š”) ๋‹ค์‹œ ์•ฑ์— ์ ‘์†ํ• ๋•Œ isAutoLogin๊ณผ ํ† ํฐ์ด ๋งŒ๋ฃŒ๋๋Š”์ง€์˜ ์—ฌ๋ถ€๋ฅผ ์‚ดํ•€ํ›„, ๋กœ๊ทธ์ธ๋ทฐ๋กœ ๊ฐˆ์ง€ ๋ฉ”์ธ๋ทฐ๋กœ ๊ฐˆ์ง€ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค!! (์ด๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ดํ•ด๊ฐ€ ์•ˆ๋˜์‹ ๋‹ค๋ฉด ๊นŠ์€ ๋‚ด์šฉ์ด๋‹ค๋ณด๋‹ˆ, PR๋ณด๋‹ค๋Š” ํ•ด๋‹น PR๋จผ์ € ์ •๋ฆฌํ›„ ๋จธ์ง€ ์‹œํ‚จ ๋‹ค์Œ, ๊ฐœ์ธ์ ์œผ๋กœ ์งˆ๋ฌธํ•ด์ฃผ์„ธ์š”..!)

์ž์„ธํ•œ ๋‹ต๋ณ€ ๋„ˆ๋ฌด ๊ณ ๋งˆ์›Œ์š”. ์•ˆ๊ทธ๋ž˜๋„ ํด๋ผ์ด์–ธํŠธ ํ˜ผ์ž์„œ ์„œ๋ฒ„์™€ ํ†ต์‹ ์„ ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋‹ค๋ฃจ๋‹ค๋ณด๋‹ˆ, ์กฐ๊ธˆ ๋ฆฌ์„œ์น˜ ํ•ด์•ผ ํ•  ๋ถ€๋ถ„๋“ค์ด ๋งŽ์•„์ง„ ๊ฒƒ ๊ฐ™์€๋ฐ, JWT๋ฅผ ์‚ฌ์šฉํ•œ ์‚ฌ์šฉ์ž ์ธ์ฆ ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ ๋ฆฌ์„œ์น˜๋ฅผ ํ•ด๋ณด๋Š” ์ค‘ ์ž…๋‹ˆ๋‹ค. ์ƒํ™ฉ ์ง€์†์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ• ๊ฒŒ์š”!