Get value for current user from Firebase Firestore - Swift 4 - swift

this is my first question here, i hope you can help me.
I've been trying to get a specific path for the current user logged into my app.
I don't really now what i'm doing wrong so i'll paste my code here.
func userRoleListener() {
guard let user = Auth.auth().currentUser else { return } .
Firestore.firestore().collection("users").document("sjbD5SvKTYHWBLhCqojs").getDocument { (snapshot, error) in
if let data = snapshot?.data() {
guard let isAdmin = data["isAdmin"] as? Bool else { return }
if isAdmin {
self.applyButton.isHidden = true
} else {
self.applyButton.isHidden = false
}
}
}
}
This is my function to create a user.
private func createUser() {
guard let nameAndLastname = nameAndLastnameTextField.text , let email = emailTextField.text , let password = passwordTextField.text , !nameAndLastname.isEmpty , !email.isEmpty , !password.isEmpty else {
simpleAlert(title: "Error", msg: "Debe completar todos los campos")
return
}
let newUserReference = Firestore.firestore().collection("users").document()
newUserReference.setData([
"nameAndLastname": nameAndLastname,
"email": email,
"password": password,
"isAdmin": false,
"timestamp": Timestamp()
])
}
And this is my login action:
#IBAction func signUpButtonPressed(_ sender: Any) {
guard let email = emailTextField.text , let password = passwordTextField.text , !email.isEmpty , !password.isEmpty else { return }
self.signUpButton.animateButton(shouldLoad: true, withMessage: "")
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if let error = error {
self.signUpButton.animateButton(shouldLoad: false, withMessage: "¡REGISTRAR!")
debugPrint(error.localizedDescription)
self.simpleAlert(title: "Error", msg: "Error al iniciar sesión, intente nuevamente en unos minutos")
return
}
self.signUpButton.animateButton(shouldLoad: false, withMessage: "")
self.createUser()
self.presentClientStoryboard()
}
}
And this is an image of my database:
Database image

If you create a user with Firebase Authentication, Authentication will create a user and a user id for you in Firebase Auth (Not Firestore)
So when you create your user in Firestore, you have to set the userID as document id, like below:
#IBAction func signUpButtonPressed(_ sender: Any) {
guard let email = emailTextField.text , let password = passwordTextField.text , !email.isEmpty , !password.isEmpty else { return }
self.signUpButton.animateButton(shouldLoad: true, withMessage: "")
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if let error = error {
self.signUpButton.animateButton(shouldLoad: false, withMessage: "¡REGISTRAR!")
debugPrint(error.localizedDescription)
self.simpleAlert(title: "Error", msg: "Error al iniciar sesión, intente nuevamente en unos minutos")
return
}
self.signUpButton.animateButton(shouldLoad: false, withMessage: "")
self.createUser(user.uid) // <----- user id from Firebase Auth
self.presentClientStoryboard()
}
}
and
private func createUser(userId: String) {
guard let nameAndLastname = nameAndLastnameTextField.text , let email = emailTextField.text , let password = passwordTextField.text , !nameAndLastname.isEmpty , !email.isEmpty , !password.isEmpty else {
simpleAlert(title: "Error", msg: "Debe completar todos los campos")
return
}
let newUserReference = Firestore.firestore().collection("users").document(userId) // <-- create a document, with the user id from Firebase Auth
newUserReference.setData([
"nameAndLastname": nameAndLastname,
"email": email,
"password": password,
"isAdmin": false,
"timestamp": Timestamp()
])
}

You should get the user id from the current user, try this:
func userRoleListener() {
guard let userUid = Auth.auth().currentUser.uid else { return } .
Firestore.firestore().collection("users").document(userUid).getDocument { (snapshot, error) in
if let data = snapshot?.data() {
guard let isAdmin = data["isAdmin"] as? Bool else { return }
if isAdmin {
// And I believe here the true and false values should be switched as you are checking if the user IS an admin, if they are an admin, shouldn't you show the button?
self.applyButton.isHidden = false
} else {
self.applyButton.isHidden = true
}
}
}
}
This get's the current user logged into the app and then uses the users uid to search the database. Just make sure when you create an account you save the user data accordingly.

Related

Does not add a user to Firebase

I am trying to add users to my Firebase cloud
I connected my project to the console.firebase
While I fill in the email and password it is not adding to my firebase.
I have the following code:
#IBAction func registerButton(_ sender: Any) {
signUp()
}
this is a button for register
and this is the func signup:
func signUp (){
let name = nameValue.text
let password = passwordValue.text
let email = emailValue.text
if (!password!.isEmpty && !email!.isEmpty) {
Auth.auth().createUser(withEmail: email ?? "", password: password ?? "") { (result, error) in
if error == nil {
if let result = result {
}
}
}
}
else {
showAlert()
}
}
Can anybody help to figure out this problem?
here is the solution to my problem:
if (!name!.isEmpty && !password!.isEmpty && !email!.isEmpty) {
Auth.auth().createUser(withEmail: email ?? "", password: password ?? "") { (authResult, error) in
if let e = error {
print(e.localizedDescription)
} else {
// self.performSegue(withIdentifier: "main", sender: self)
}
}
}

How do I update UILabels synchronously with Firestore data?

I'm currently building an iOS app that will synchronize account information from Firestore. I have the login/register process hooked up and working. However, I need help understanding how to update my logInOutBtn, fullNameTxt and emailTxt in my MenuVC automatically when an user logs in/out. Currently, it will update whenever I close then reopen the menu, but what should I use to automatically update it without having to close the menu? Thanks!
// MenuVC
override func viewDidAppear(_ animated: Bool) {
if let user = Auth.auth().currentUser , !user.isAnonymous {
// We are logged in
logInOutBtn.setTitle("Logout", for: .normal)
if UserService.userListener == nil {
UserService.getCurrentUser {
self.fullNameTxt.text = UserService.user.fullName
self.emailTxt.text = UserService.user.email
}
}
} else {
logInOutBtn.setTitle("Login", for: .normal)
self.fullNameTxt.text = "Sign in or create an account"
self.emailTxt.text = "to continue."
}
}
fileprivate func presentLoginController() {
let storyboard = UIStoryboard(name: Storyboard.LoginStoryboard, bundle: nil)
if #available(iOS 13.0, *) {
let controller = storyboard.instantiateViewController(identifier: StoryboardId.LoginVC)
present(controller, animated: true, completion: nil)
} else {
// Fallback on earlier versions
}
}
#IBAction func logInOutClicked(_ sender: Any) {
guard let user = Auth.auth().currentUser else { return }
if user.isAnonymous {
presentLoginController()
} else {
do {
try Auth.auth().signOut()
UserService.logoutUser()
Auth.auth().signInAnonymously { (result, error) in
if let error = error {
debugPrint(error)
Auth.auth().handleFireAuthError(error: error, vc: self)
}
self.presentLoginController()
}
} catch {
debugPrint(error)
Auth.auth().handleFireAuthError(error: error, vc: self)
}
}
}
// UserService
func getCurrentUser(completion: #escaping () -> ()) {
guard let authUser = auth.currentUser else { return }
let userRef = db.collection("users").document(authUser.uid)
userListener = userRef.addSnapshotListener({ (snap, error) in
if let error = error {
debugPrint(error.localizedDescription)
return
}
guard let data = snap?.data() else { return }
self.user = User.init(data: data)
completion()
})
// User Model
struct User {
var fullName: String
var address: String
var id: String
var email: String
var stripeId: String
init(fullName: String = "",
address: String = "",
id: String = "",
email: String = "",
stripeId: String = "") {
self.fullName = fullName
self.address = address
self.id = id
self.email = email
self.stripeId = stripeId
}
init(data: [String : Any]) {
fullName = data["fullName"] as? String ?? ""
address = data["address"] as? String ?? ""
id = data["id"] as? String ?? ""
email = data["email"] as? String ?? ""
stripeId = data["stripeId"] as? String ?? ""
}
static func modelToData(user: User) -> [String : Any] {
let data : [String : Any] = [
"fullName" : user.fullName,
"address" : user.address,
"id" : user.id,
"email" : user.email,
"stripeId" : user.stripeId
]
return data
}
}
// My app menu
The signout process is pretty straightforward and is marked as throws so if it fails, it will generate an error that can be handled by a catch. It is not asynchronous so it won't have (or need) a closure.
So simply stated
func signOut() {
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
print("successful signout")
self.logInOutBtn.setTitle("Log In", for: .normal)
self.fullNameTxt.text = ""
self.emailTxt.text = ""
} catch let signOutError as NSError {
print ("Error signing out: %#", signOutError)
//present the error to the user/handle the error
}
}
The signIn function is asynchronous with a closure so when the user signs in successfully, the code in the closure will fire and that's the perfect place to update the UI.
Auth.auth().signIn(withEmail: email, password: password) { [weak self] authResult, error in
guard let strongSelf = self else { return }
// update the UI here.
}
You can also just monitor the authState with an observer and have it react to users logging in/out
self.authListener = Auth.auth()?.addAuthStateDidChangeListener { auth, user in
if let theUser = user {
print("User logged in \(theUser)") // User is signed in.
self.dismissViewControllerAnimated(true, completion: nil)
} else {
print("Need to login.") // No user is signed in.
//present login view controller
}
}
If you no longer want to observe the auth state, you can remove it with
Auth.auth()?.removeAuthStateDidChangeListener(self.authListener)

Sign up still successful even though username is taken

I want to create a system where the user can't sign up if the username is already taken by others. However, this seems to not work as even though the username is taken, the sign up process is still completed.
I am doing this by retrieving every username from Firebase and running it through it see if the username entered by the user is already used. If it is, it should stop the sign up process and tell user to enter another username.
Code:
let ref = Database.database().reference(fromURL: "https://heytest.firebaseio.com/")
let usernamesRef = ref.child("users")
usernamesRef.observeSingleEvent(of: .value, with: { (snapshot) in
let con = snapshot.value as! [String:[String:Any]]
var usernamesArray = [String]()
Array(con.keys).forEach {
if let res = con[$0] , let username = res["username"] as? String {
usernamesArray.append(username)
}
}
for storedUsername in usernamesArray {
if storedUsername == self.usernameTextField.text! {
self.usernameVerified = false
self.usernameLabel.textColor = UIColor.red
self.usernameLabel.text = "USERNAME TAKEN"
self.usernameTextField.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 1.5)
return
}
}
}) { (error) in
print(error.localizedDescription)
}
print("TEST")
self.usernameVerified = true
print(self.usernameVerified)
self.usernameTextField.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.black, thickness: 1.5)
self.usernameLabel.textColor = UIColor.black
self.usernameLabel.text = "USERNAME"
On sign in button clicked:
#IBAction func onSignUp(_ sender: Any) {
print("Sign Up pressed")
isValidUsername(username: usernameTextField.text!)
print("[SIGN UP] - Username: \(usernameVerified)")
isValidEmail(email: emailTextField.text!)
print("[SIGN UP] - Email: \(emailVerified)")
isValidPassword(password: passwordTextField.text!)
print("[SIGN UP] - Password: \(passwordVerified)")
if passwordVerified && emailVerified && usernameVerified {
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (authResult, error) in
if error != nil {
self.errorLabel.alpha = 1
self.errorLabel.text = error?.localizedDescription
self.shake(viewToShake: self.errorLabel)
return
}
guard let user = authResult?.user else {
return
}
//Successfully Authenticated User
let ref = Database.database().reference(fromURL: "https://heytest.firebaseio.com/")
let usersReference = ref.child("users").child(user.uid)
let values = ["username": self.usernameTextField.text!, "email": self.emailTextField.text!, "games-played": "0"]
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!)
return
}
//Successfully registered user's data to database
print("[SIGN UP] - Successfully Signed Up")
self.errorLabel.alpha = 0
self.present((self.storyboard?.instantiateViewController(withIdentifier: "TabBarViewController"))!, animated: false, completion: nil)
})
}
} else {
errorLabel.alpha = 1
shake(viewToShake: errorLabel)
print("Password/Email/Username verification not complete!")
print("[SIGN UP] - Password: \(passwordVerified)")
print("[SIGN UP] - Username: \(usernameVerified)")
print("[SIGN UP] - Email: \(emailVerified)")
}
}
I think it will be easier for you to see if any matches are thrown from firebase for a specific userName.
let reference = Database.database().reference()
reference.child("users").queryOrdered(byChild: "username").queryEqual(toValue: "yourNewUserName").observeSingleEvent(of: .value) { (snapshot) in
// if there is data in the snapshot reject the registration else allow it
}
Otherwise you are downloading way too much data to the client. If you grow on users it will also take forever to download.
I am doing this by retrieving every username from Firebase and running it through it see if the username entered by the user is already used.
IMHO this is not the right approach, the logic should be handled server side. Imagine if you have 1 million user this will never work.

Save User's name in Firebase - Swift

I have three text field in my registration form. The e-mail id and password of the user are used by the Firebase SignUp method to create a new user. But I also want to save the user's name according to what they input.
My current code is;
#IBAction func registerPressed(_ sender: Any) {
SVProgressHUD.show(withStatus: "Setting you up")
dismissUIElements(value: false)
let currentUserName = userName.text
if currentUserName?.isEmpty == false {
FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
if error != nil {
print(error!)
SVProgressHUD.dismiss()
self.dismissUIElements(value: true)
} else {
print("Registration Successful!")
SVProgressHUD.dismiss()
self.dismissUIElements(value: true)
self.performSegue(withIdentifier: "goToSelectionFromRegister", sender: self)
}
})
}
else {
SVProgressHUD.dismiss()
SVProgressHUD.showError(withStatus: "Please enter your name!")
SVProgressHUD.dismiss(withDelay: 1)
self.dismissUIElements(value: true)
}
}
You need a function that will register the user and then create the child for that user in Firebase.
let databaseRef=//your path to users.
func registerUser(userUsername userName:String, userEmail email:String, userPassword password: String, userCreationComplete: #escaping (_ status: Bool, _ error: Error?) -> ()) {
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
guard let user = user else {
userCreationComplete(false, error)
return
}
let userData = ["userName": userName.text] as [String : Any]
ref.child(user.uid).updateChildValues(userData)
userCreationComplete(true, nil)
}
}
Then you call the function from within registerPressed() and you pass it the textfield values but make sure that none of them is empty.
You will create a new data table that stores that info. It won't be done in the create user function.
// create a reference to the DB
ref = Database.database().reference(fromURL: "https://your-project-name.firebaseio.com/")
//variables to store data
var myID : String?
var myName : String?
var myNumber : String?
var myEmail : String?
// creating the save user function
func saveUser(_ completion: #escaping(_ error: Error?) -> Void) {
if PageDataSource.sharedInstance.crudIsAvailable == true {
let usersRef = ref.child("users")
let myUserRef = usersRef.child(id)
myUserRef.updateChildValues(["User ID": id,
"Name": myName,
"Email": myEmail,
"Phone": .myNumber], withCompletionBlock: { (error, ref) in
if error != nil {
completion(error!)
} else {
completion(nil)
}
})
} else {
completion(NSError(domain: "Unavailable", code: 0, userInfo: nil))
}
}
// call the method like this to perform the save
func storeUser(completion: #escaping(_ completed: Bool, _ error: NSError?)-> Void) {
if let user = Auth.auth().currentUser {
myID = user.uid
myName = user.displayName
myEmail = user.email
// etc.,
completion(true,nil)
} else {
completion(false,NSError(domain: "No Current User", code: 1, userInfo: nil))
}
}

Swift Firebase Facebook Login - Gets Name, but email returns nil

Recently my facebook login hasn't been working. It doesn't save into my database but in Firebase authentication it shows someone signed in with facebook but the email is blank. It doesn't save any data to my database which it should. The email is returning nil. My google sign in works. I've checked all the connections and they all seem to be fine. (I could have missed something) anyone have any suggestions on what connections I should check? Not sure what to do...
More Info
I printed my graph request... not sure if this helps to debug
let loginManager: FBSDKLoginManager = FBSDKLoginManager()
loginManager.logIn(withReadPermissions: self.facebookPermissions, from: self, handler: { (result, error) in
if error != nil {
loginManager.logOut()
let message: String = "An error has occured. \(String(describing: error))"
let alertView = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.alert)
alertView.addAction(UIAlertAction(title: "Ok ", style: UIAlertActionStyle.default, handler: nil))
self.present(alertView, animated: true, completion: nil)
} else if (result?.isCancelled)! {
// user cancelled login
loginManager.logOut()
} else {
let accessToken = FBSDKAccessToken.current()
guard let accessTokenString = accessToken?.tokenString else { return }
let credential = FacebookAuthProvider.credential(withAccessToken: accessTokenString)
Auth.auth().signIn(with: credential) { (user, error) in
if (error != nil) {
// handle error
print(error ?? "Error")
} else {
let ref = Database.database().reference()
// guard for user id
guard let uid = user?.uid else {
return
}
let usersReference = ref.child("user_profiles").child(uid)
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"])
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if error != nil {
// Process error
print("Error: \(String(describing: error))")
} else {
guard let data: [String:AnyObject] = result as? [String:AnyObject] else {
print("Can't pull data from JSON")
return
}
guard let userName: String = data["name"] as? String else {
print("Can't pull username from JSON")
return
}
guard let userID: String = data["id"] as? String else {
print("Can't pull ID from JSON")
return
}
let imgURLString = "http://graph.facebook.com/\(userID)/picture?type=large" as String
guard let userEmail: String = data["email"] as? String else {
print("Can't pull email from JSON")
print("Error: \(String(describing: error))")
return
}
// initial # posts = 0
let values = ["name": userName, "email": userEmail, "facebookID": userID, "profPicString": imgURLString] as [String : Any]
// update database with new user
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
// error in database save
if err != nil {
print(err ?? "Error saving user to database")
return
}
})
}
})
self.dismiss(animated: false, completion: nil)
// Present the main view
if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Customer Profile") {
UIApplication.shared.keyWindow?.rootViewController = viewController
self.dismiss(animated: true, completion: nil)
}
}
}
}
})
Using Swift 5 I found the email inside providerData which is an array of FIRUserInfo:
if AccessToken.current != nil {
let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
Auth.auth().signIn(with: credential) { (res, err) in
if err != nil || res == nil {
//...
return
}
guard let providerData = res?.user.providerData else {
//...
return
}
for firUserInfo in providerData {
print(firUserInfo.providerID)
print(firUserInfo.email ?? "Email not found")
}
}
}