Reputation: 31
I have an app that allows users to create new accounts or login through facebook/Google. Some minor data like profile name etc are stored in user defaults. I am using Firebase for the logins.
Issue: After user A logs out of their account and user B logs in, the data (saved into user defaults) of user A is visible to user B.
What I want: Obviously that should not happen and user B Should not
see user A data. How can I make this work?
Logout code
@IBAction func logout(_ sender: Any) {
if Auth.auth().currentUser != nil {
do {
try Auth.auth().signOut()
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
present(vc, animated: true, completion: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
}else {
GIDSignIn.sharedInstance()?.signOut()
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
present(vc, animated: true, completion: nil)
}
let firebaseAuth = Auth.auth()
do {
print("logged out success!")
try firebaseAuth.signOut()
} catch let signOutError as NSError {
print ("Error signing out: %@", signOutError)
}
}
Edit: I have just started storing UID into userdefaults.
Upvotes: 0
Views: 373
Reputation: 6190
Besides the fact that you can store Data
objects in your UserDefaults
, meaning that could be concrete Codable
structures, you can also store dictionaries
in UserDefaults.
So let's say you have two users, you need to store information about, and the way to identify this user is by username.
This would be your storing code, you see how you "pack" all the details you need into Dictionary
and store it in the defaults by username. Meaning whenever you need to retrieve info you just need a username.
var userDict: [String: Any] = [:]
let username: String = "user@name"
let age: Int = 25
let language: String = "en"
userDict = ["username": username,
"age": age,
"language": language]
UserDefaults.standard.set(userDict, forKey: username)
And to retrieve this dictionary in the future with all the information you need:
let userData = UserDefaults.standard.dictionary(forKey: username)
Now to get your stuff from it:
let age = userData["age"] // 25
let language = userData["language"] // en
Upvotes: 1