Reputation: 463
I wan't to check if the user is't logged into my app. If that happend, i wan't to redirect him to notLoggedView or verifyAccuontView.
At this moment code looks like this and if user not created i can't get into notLoggedView.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true);
let user = Auth.auth().currentUser;
if (user == nil) {
self.performSegue(withIdentifier: "notLoggedView", sender: self);
}
}
This code works same as above (like user exist, but firebase accounts is empty)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true);
if let user = Auth.auth().currentUser {
// action when account exist
} else {
self.performSegue(withIdentifier: "notLoggedView", sender: self);
}
}
Upvotes: 4
Views: 4991
Reputation: 14397
override func viewDidLoad() {
super.viewDidLoad()
Auth.auth()!.addStateDidChangeListener() { auth, user in
if user != nil {
self.switchStoryboard()
}
}
}
or You can check directly
if Auth.auth().currentUser?.uid != nil {
//user is logged in
}else{
//user is not logged in
}
for Signup you use
Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!) { user, error in
if error == nil {
// 3
Auth.auth().signIn(withEmail: self.textFieldLoginEmail.text!,
password: self.textFieldLoginPassword.text!)
}
}
Upvotes: 7
Reputation: 317362
If you want to check if there is no user logged in, check currentUser
directly.
let user = Auth.auth().currentUser;
if (user == nil) {
// there is no user signed in when user is nil
}
Also be sure see the documentation for more information.
Upvotes: 2