Last active
March 22, 2020 18:36
-
-
Save hrvolapeter/37074392c80dbddb48bffff2e08161a4 to your computer and use it in GitHub Desktop.
Zetten authentication service part 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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