Max
Max

Reputation: 629

What is the appropriate way to store Firebase Authentication info so you can leave the app and come back still signed in?

I followed a few examples online and now have a SessionData class that stores auth details, etc. When you fill out fields for email and password on the login screen, it updates the email and password in SessionData and then uses that to authenticate with Firebase. I have authentication working fine, but I can't figure out how to store authentication locally on the device so people don't have to sign-in again if they leave the app. The example I found shows @AppStorage for the logged_in status, but I've read that @AppStorage should not be used for usernames and passwords due to security issues. Any thoughts?

class SessionData : ObservableObject {
    @Published var remember = true
    @Published var email = ""
    @Published var password = ""
    @Published var isSignUp = false
    @Published var email_SignUp = ""
    @Published var password_SignUp = ""
    @Published var confirmPassword = ""
    @Published var isLinkSend = false
    @Published var alert = false
    @Published var alertMsg = ""
    @AppStorage("log_Status") var logged_in = false
    @Published var isLoading = false

    func login(){
        
        // checking all fields are inputted correctly...
        
        if email == "" || password == ""{
            
            self.alertMsg = "Please enter email and password!"
            self.alert.toggle()
            return
        }
        
        withAnimation{
            
            self.isLoading.toggle()
        }
        
        Auth.auth().signIn(withEmail: email, password: password) { (result, err) in
        ...
...
}

Upvotes: 0

Views: 200

Answers (1)

Max
Max

Reputation: 629

Ah, of course I figured it out right after posting. I wanted to leave this here since I couldn't find this asked anywhere.

Link: https://github.com/kishikawakatsumi/KeychainAccess

It's a great Keychain access wrapper that makes it very simple to store/retrieve data securely.

Upvotes: 1

Related Questions