Skip to content

Commit

Permalink
[FIX] TeamNADA#246 - 자동 로그인 최종 구현완료
Browse files Browse the repository at this point in the history
  • Loading branch information
mini-min committed Dec 28, 2021
1 parent 51d19d8 commit af444d6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 71 deletions.
106 changes: 37 additions & 69 deletions NADA-iOS-forRelease/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,47 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

removeTokenAtFirstLaunch()
KakaoSDKCommon.initSDK(appKey: "5b8dd8cc878344bb7532eeca4365a4aa")

let acToken = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken)
let rfToken = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.refreshToken)

if acToken != "" {
postUserTokenReissue(request: UserTokenReissueRequset(accessToken: acToken ?? "", refreshToken: rfToken ?? ""))
if acToken != nil {
if UserDefaults.standard.bool(forKey: Const.UserDefaultsKey.isAppleLogin) {
// 애플 로그인으로 연동되어 있을 때, -> 애플 ID와의 연동상태 확인 로직
let appleIDProvider = ASAuthorizationAppleIDProvider()
appleIDProvider.getCredentialState(forUserID: Const.UserDefaultsKey.userID) { (credentialState, error) in
switch credentialState {
case .authorized:
print("해당 ID는 연동되어있습니다.")
self.isLogin = true
case .revoked:
print("해당 ID는 연동되어있지않습니다.")
self.isLogin = false
case .notFound:
print("해당 ID를 찾을 수 없습니다.")
self.isLogin = false
default:
break
}
}
} else {
if AuthApi.hasToken() { // 유효한 토큰 존재
UserApi.shared.accessTokenInfo { (_, error) in
if let error = error {
if let sdkError = error as? SdkError, sdkError.isInvalidTokenError() == true {
self.isLogin = false
}
} else {
// 토큰 유효성 체크 성공(필요 시 토큰 갱신됨)
self.isLogin = true
}
}
} else {
// 카카오 토큰 없음 -> 로그인 필요
self.isLogin = false
}
}
} else {
self.isLogin = false
self.isLogin = false // acToken 값이 nil일 때 -> 로그인 뷰로
}

// 앱 실행 중 애플 ID 강제로 연결 취소 시
Expand All @@ -40,16 +71,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

private func removeTokenAtFirstLaunch() {
guard UserDefaults.isFirstLaunch() else {
return
}
UserDefaults.standard.removeObject(forKey: Const.UserDefaultsKey.accessToken)
UserDefaults.standard.removeObject(forKey: Const.UserDefaultsKey.refreshToken)
UserDefaults.standard.removeObject(forKey: Const.UserDefaultsKey.isKakaoLogin)
UserDefaults.standard.removeObject(forKey: Const.UserDefaultsKey.isAppleLogin)
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
Expand All @@ -63,59 +84,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

private func postUserTokenReissue(request: UserTokenReissueRequset) {
UserAPI.shared.userTokenReissue(request: request) { response in
switch response {
case .success:
print("postUserTokenReissue - Success")
if UserDefaults.standard.bool(forKey: Const.UserDefaultsKey.isAppleLogin) {
// 애플 로그인으로 연동되어 있을 때, -> 애플 ID와의 연동상태 확인 로직
let appleIDProvider = ASAuthorizationAppleIDProvider()
appleIDProvider.getCredentialState(forUserID: Const.UserDefaultsKey.userID) { (credentialState, error) in
switch credentialState {
case .authorized:
print("해당 ID는 연동되어있습니다.")
self.isLogin = true
case .revoked:
print("해당 ID는 연동되어있지않습니다.")
self.isLogin = false
case .notFound:
print("해당 ID를 찾을 수 없습니다.")
self.isLogin = false
default:
break
}
}
} else {
if AuthApi.hasToken() { // 유효한 토큰 존재
UserApi.shared.accessTokenInfo { (_, error) in
if let error = error {
if let sdkError = error as? SdkError, sdkError.isInvalidTokenError() == true {
self.isLogin = false
}
} else {
// 토큰 유효성 체크 성공(필요 시 토큰 갱신됨)
self.isLogin = true
}
}
} else {
// 카카오 토큰 없음 -> 로그인 필요
self.isLogin = false
}
}
case .requestErr(let message):
print("postUserTokenReissue - requestErr: \(message)")
self.isLogin = false
case .pathErr:
print("postUserTokenReissue - pathErr")
case .serverErr:
print("postUserTokenReissue - serverErr")
case .networkFail:
print("postUserTokenReissue - networkFail")
}
}
}
}

extension UserDefaults {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ extension MoreViewController {
self.makeOKAlert(title: "", message: "로그아웃이 완료 되었습니다.") { _ in
if let acToken = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken) {
self.logoutUserWithAPI(token: acToken)
self.defaults.set(false, forKey: Const.UserDefaultsKey.hasBeenLaunchedBeforeFlag)
self.defaults.removeObject(forKey: Const.UserDefaultsKey.accessToken)
self.defaults.removeObject(forKey: Const.UserDefaultsKey.refreshToken)
self.defaults.removeObject(forKey: Const.UserDefaultsKey.darkModeState)
let nextVC = UIStoryboard(name: Const.Storyboard.Name.login, bundle: nil).instantiateViewController(withIdentifier: Const.ViewController.Identifier.loginViewController)
nextVC.modalPresentationStyle = .overFullScreen
Expand Down Expand Up @@ -146,7 +147,8 @@ extension MoreViewController {
self.makeOKAlert(title: "", message: "모든 명함이 삭제되었습니다.") { _ in
if let acToken = UserDefaults.standard.string(forKey: Const.UserDefaultsKey.accessToken) {
self.deleteUserWithAPI(token: acToken)
self.defaults.set(false, forKey: Const.UserDefaultsKey.hasBeenLaunchedBeforeFlag)
self.defaults.removeObject(forKey: Const.UserDefaultsKey.accessToken)
self.defaults.removeObject(forKey: Const.UserDefaultsKey.refreshToken)
self.defaults.removeObject(forKey: Const.UserDefaultsKey.darkModeState)
let nextVC = UIStoryboard(name: Const.Storyboard.Name.login, bundle: nil).instantiateViewController(withIdentifier: Const.ViewController.Identifier.loginViewController)
nextVC.modalPresentationStyle = .overFullScreen
Expand Down

0 comments on commit af444d6

Please sign in to comment.