I use my login logic using Firebase using only Facebook as a provider.
How can I save my CurrentUser after logging in to use my personal data while using the application later?
I am currently using singleton with a User instance. Something like that:
CurrentUser.swift
class CurrentUser { static let i: CurrentUser = CurrentUser() var cUser: User? private init() { } func setCurrentUser(u: User) { cUser = u } func getCurrentUser() -> User { return cUser! } func clearUser() { cUser = nil } func userIsLogged() -> Bool { return cUser != nil } }
And I use this singleton as follows:
LoginViewController.swift
class LoginViewController: UIViewController { ... func createCurrentUser(authData: FAuthData) { let u = User(uid: authData.uid, displayName: authData.providerData["displayName"] as! String, email: authData.providerData["email"] as! String) u.wrapperFromFacebookData(authData.providerData) ref.childByAppendingPath("users").childByAppendingPath(u.uid).setValue(u.toDict()) CurrentUser.i.setCurrentUser(u) } ... }
I do not think this is the best practice. Before Firebase, I used to deal with Parse user logic, which was pretty easy.
source share