1

If i understood correct the user UID its this is a unique uid, until the user logs out. I mean he can close/open the app many times and user UID must be the same.

I have test class:

class UserFirebase {
    
    func authorization(completionHandler: @escaping (Result<AuthDataResult?, Error>) -> Void) {
        Auth.auth().signInAnonymously { authResult, error in
            if error == nil {
                completionHandler(.success(authResult))
                return
            }
            
            completionHandler(.failure(error))
        }
    }
    
    func singOut() {
        try? Auth.auth().signOut()
    }
    
    func getUserUUID() -> String? {
        return Auth.auth().currentUser?.uid
    }
    
    func isAuthorized() -> Bool {
        return Auth.auth().currentUser != nil
    }
}

when app is running i using this class like this:

let userFirebaseManager: UserFirebase = UserFirebase()
        
        if userFirebaseManager.isAuthorized() {
            // make something
        } else {
            userFirebaseManager.authorization(completionHandler: {[weak self] (result) in
                // make something
            })
        }

every time I start the app, the user isAuthorized() == false. Shouldn't it persist until I press logout?

UPD: why does my currentUser.uid change every time I restart the application?

vetaney915
  • 23
  • 3
  • It's been over a month since you posted this question. Please mark a correct answer or give us new information so we can help you. – SwiftCODA Sep 13 '21 at 21:08

2 Answers2

1

The UID of an anonymous user is deleted when the user signs out. It cannot be regained after that, not even when you sign in on the same device.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • but i just restart the app and my user.uid changes every time – vetaney915 Aug 12 '21 at 14:39
  • Hmm... that shouldn't be the case. You might want to first [get the currently signed in user](https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user) with `addStateDidChangeListener` before calling `signInAnonymously`. I don't think that should be required, but something may have have changed there. – Frank van Puffelen Aug 12 '21 at 15:00
0

Anonymous users are signed out once the app is closed. I'm not sure why you need to sign in anonymously AND keep that UUID known. If you need to sign in anonymously just to securely access your Firebase project, it shouldn't matter if the user gets signed out. If you want to preserve the UUID, the user needs to create an account with Auth.auth().createUser(withEmail: String, password: String, completion: ((AuthDataResult?, Error?) -> Void)

SwiftCODA
  • 172
  • 2
  • 16