Skip to content

Instantly share code, notes, and snippets.

@hrvolapeter
Last active March 22, 2020 18:36
Show Gist options
  • Save hrvolapeter/37074392c80dbddb48bffff2e08161a4 to your computer and use it in GitHub Desktop.
Save hrvolapeter/37074392c80dbddb48bffff2e08161a4 to your computer and use it in GitHub Desktop.
Zetten authentication service part 5
import Firebase
class AuthenticationService: ObservableObject {
@Published var user: User?
var cancellable: AuthStateDidChangeListenerHandle?
init() {
cancellable = Auth.auth().addStateDidChangeListener { (_, user) in
if let user = user {
logger.event("Got user: \(user.uid)")
self.user = User(
uid: user.uid,
email: user.email,
displayName: user.displayName
)
} else {
self.user = nil
}
}
}
func signUp(
email: String,
password: String,
handler: @escaping AuthDataResultCallback
) {
Auth.auth().createUser(withEmail: email, password: password, completion: handler)
}
func signIn(
email: String,
password: String,
handler: @escaping AuthDataResultCallback
) {
Auth.auth().signIn(withEmail: email, password: password, completion: handler)
}
func signOut() throws {
try Auth.auth().signOut()
self.user = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment