programing

Firebase - 앱을 삭제하고 다시 설치해도 사용자 인증이 취소되지 않습니다.

sourcejob 2023. 6. 8. 19:38
반응형

Firebase - 앱을 삭제하고 다시 설치해도 사용자 인증이 취소되지 않습니다.

다음 코드로 사용자를 인증한 후(아래는 내 코드의 잘린 버전이므로 성공적인 로그인 로직만 표시됨)...

let firebaseReference = Firebase(url: "https://MY-FIREBASE.firebaseio.com")

 

FBSession.openActiveSessionWithReadPermissions(["public_profile", "user_friends"], allowLoginUI: true,
    completionHandler: { session, state, error in

        if state == FBSessionState.Open {
            let accessToken = session.accessTokenData.accessToken
            firebaseReference.authWithOAuthProvider("facebook", token: accessToken,
                withCompletionBlock: { error, authData in

                    if error != nil {
                        // Login failed.
                    } else {
                        // Logged in!
                        println("Logged in! \(authData)")
                    }
            })
        }
    })
}

(즉, 앱 실행 및 실행, 로그인 성공).

그런 다음 앱을 삭제하고 동일한 장치에 다시 설치하면 사용자가 로그인했는지 여부를 확인하기 위해 앱 대리자에서 사용하는 이 통화는 항상 로그인했다는 메시지를 반환합니다.

if firebaseReference.authData == nil {
    // Not logged in
} else {
    // Logged in
}

왜 그런 것일까요?앱을 삭제하고 다시 설치하면 모든 데이터가 지워진다고 생각했을 것입니다.

iOS 시뮬레이터에서 콘텐츠 및 설정을 재설정하고 앱을 설치하면,firebaseReference.authData재산은 다시 한 번 될 것입니다.nil.

Firebase 인증 세션은 iOS 키 체인의 사용자 장치에서 지속됩니다.응용 프로그램의 키 체인 데이터는 응용 프로그램을 제거할 때 제거되지 않습니다.

데이터를 수동으로 지우려는 경우 응용 프로그램과 함께 일부 추가 메타데이터를 저장하고 수동으로 호출할 수 있습니다.FirebaseRef.unauth()지속된 세션을 지웁니다.#4747404: 추가 참조를 위해 앱을 제거할 때 키 체인 항목 삭제를 참조하십시오.

didd 마지막에 아래 코드를 추가하면 AppDelegate의 FinishLaunchingWithOptions 기능(true로 돌아가기 전)이 빠르게 작동합니다.

신속한 방법

let userDefaults = UserDefaults.standard
if userDefaults.value(forKey: "appFirstTimeOpend") == nil {
    //if app is first time opened then it will be nil
    userDefaults.setValue(true, forKey: "appFirstTimeOpend")
    // signOut from Auth
    do {
        try Auth.auth().signOut()
    }catch {
        
    }
    // go to beginning of app
} else {
    //go to where you want
}

스위프트 3.*

let userDefaults = NSUserDefaults.standardUserDefaults()
if userDefaults.valueForKey("appFirstTimeOpend") == nil {
    //if app is first time opened then it will be nil
    userDefaults.setValue(true, forKey: "appFirstTimeOpend")
    // signOut from FIRAuth
    do {
        try FIRAuth.auth()?.signOut()
    }catch {
    
    }
    // go to beginning of app
} else {
   //go to where you want
}

swift 4의 경우 동일한 답변:

let userDefaults = UserDefaults.standard
if userDefaults.value(forKey: "appFirstTimeOpend") == nil {
    //if app is first time opened then it will be nil
    userDefaults.setValue(true, forKey: "appFirstTimeOpend")
    // signOut from FIRAuth
    do {
        try Auth.auth().signOut()
    }catch {

    }
    // go to beginning of app
} else {
    //go to where you want
}

아래 확장자 사용:

extension AppDelegate{
func signOutOldUser(){
    if let _ = UserDefaults.standard.value(forKey: "isNewuser"){}else{
        do{
            UserDefaults.standard.set(true, forKey: "isNewuser")
            try Auth.auth().signOut()
        }catch{}
    }
} 
}

'... 옵션으로 시작을 마쳤습니다...App 위임 방법:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    signOutOldUser()
    return true
}

언급URL : https://stackoverflow.com/questions/27893418/firebase-deleting-and-reinstalling-app-does-not-un-authenticate-a-user

반응형