Get value for current user from Firebase Firestore - Swift 4How to call Objective-C code from SwiftFirebase iOS returning when creating usersWhat's the difference between Cloud Firestore and the Firebase Realtime Database?Firebase firestore collection countFirebase Firestore Pagination with SwiftCannot convert value of type '(User?, _) -> ()' to expected argument type.i am struggling to resolve that errorUserInfo=NSLocalizedDescription=The email address is already in use by another account., error_name=ERROR_EMAIL_ALREADY_IN_USERegister screen won't successfully segue to a new view controller.Firebase and FireStore promise in swift?Swift firebase firestore query

A life of PhD: is it feasible?

Placement of positioning lights on A320 winglets

Part of my house is inexplicably gone

As easy as Three, Two, One... How fast can you go from Five to Four?

How to make a composition of functions prettier?

Was planting UN flag on Moon ever discussed?

Is Jesus the last Prophet?

ASCII Meme Arrow Generator

Is Dumbledore a human lie detector?

Professor Roman loves to teach unorthodox Chemistry

Have minipage take up entire page height

How (un)safe is it to ride barefoot?

If I had a daughter who (is/were/was) cute, I would be very happy

What does "lit." mean in boiling point or melting point specification?

How to Handle Many Times Series Simultaneously?

Are regulatory compliance checks performed within the EU bloc?

How to handle when PCs taste a potion that is actually poison?

Forgot passport for Alaska cruise (Anchorage to Vancouver)

Why did the World Bank set the global poverty line at $1.90?

Realistic, logical way for men with medieval-era weaponry to compete with much larger and physically stronger foes

My mom's return ticket is 3 days after I-94 expires

How to befriend someone who doesn't like to talk?

Why do (or did, until very recently) aircraft transponders wait to be interrogated before broadcasting beacon signals?

How many sets of dice do I need for D&D?



Get value for current user from Firebase Firestore - Swift 4


How to call Objective-C code from SwiftFirebase iOS returning when creating usersWhat's the difference between Cloud Firestore and the Firebase Realtime Database?Firebase firestore collection countFirebase Firestore Pagination with SwiftCannot convert value of type '(User?, _) -> ()' to expected argument type.i am struggling to resolve that errorUserInfo=NSLocalizedDescription=The email address is already in use by another account., error_name=ERROR_EMAIL_ALREADY_IN_USERegister screen won't successfully segue to a new view controller.Firebase and FireStore promise in swift?Swift firebase firestore query






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















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










share|improve this question



















  • 2





    What is going wrong when you run this code?

    – Frank van Puffelen
    Mar 24 at 23:04











  • The issue i'm having is when i try this: Firestore.firestore.collection("users").document(user.uid).getDocument() I don't receibe any current user, but when i go straight forward and insert manually the current userd id ("sjbD5SvKTYHWBLhCqojs"), the button is hidden and also get the data form Firestore.

    – Leonardo Barroeta
    Mar 24 at 23:14


















2















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










share|improve this question



















  • 2





    What is going wrong when you run this code?

    – Frank van Puffelen
    Mar 24 at 23:04











  • The issue i'm having is when i try this: Firestore.firestore.collection("users").document(user.uid).getDocument() I don't receibe any current user, but when i go straight forward and insert manually the current userd id ("sjbD5SvKTYHWBLhCqojs"), the button is hidden and also get the data form Firestore.

    – Leonardo Barroeta
    Mar 24 at 23:14














2












2








2








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










share|improve this question
















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







swift firebase google-cloud-firestore






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 23:47







Leonardo Barroeta

















asked Mar 24 at 22:59









Leonardo BarroetaLeonardo Barroeta

134




134







  • 2





    What is going wrong when you run this code?

    – Frank van Puffelen
    Mar 24 at 23:04











  • The issue i'm having is when i try this: Firestore.firestore.collection("users").document(user.uid).getDocument() I don't receibe any current user, but when i go straight forward and insert manually the current userd id ("sjbD5SvKTYHWBLhCqojs"), the button is hidden and also get the data form Firestore.

    – Leonardo Barroeta
    Mar 24 at 23:14













  • 2





    What is going wrong when you run this code?

    – Frank van Puffelen
    Mar 24 at 23:04











  • The issue i'm having is when i try this: Firestore.firestore.collection("users").document(user.uid).getDocument() I don't receibe any current user, but when i go straight forward and insert manually the current userd id ("sjbD5SvKTYHWBLhCqojs"), the button is hidden and also get the data form Firestore.

    – Leonardo Barroeta
    Mar 24 at 23:14








2




2





What is going wrong when you run this code?

– Frank van Puffelen
Mar 24 at 23:04





What is going wrong when you run this code?

– Frank van Puffelen
Mar 24 at 23:04













The issue i'm having is when i try this: Firestore.firestore.collection("users").document(user.uid).getDocument() I don't receibe any current user, but when i go straight forward and insert manually the current userd id ("sjbD5SvKTYHWBLhCqojs"), the button is hidden and also get the data form Firestore.

– Leonardo Barroeta
Mar 24 at 23:14






The issue i'm having is when i try this: Firestore.firestore.collection("users").document(user.uid).getDocument() I don't receibe any current user, but when i go straight forward and insert manually the current userd id ("sjbD5SvKTYHWBLhCqojs"), the button is hidden and also get the data form Firestore.

– Leonardo Barroeta
Mar 24 at 23:14













2 Answers
2






active

oldest

votes


















1














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()
])






share|improve this answer























  • This one worked, i didn't know that i have to pass the userid on the create action. Thanks a lot.

    – Leonardo Barroeta
    Mar 24 at 23:53


















0














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.






share|improve this answer























  • I've tried this before, and i have the same issue, it didn't work.

    – Leonardo Barroeta
    Mar 24 at 23:29











  • Can you please update your code with when a user signs up, and maybe also a photo of your database?

    – OkiRules
    Mar 24 at 23:31











  • Yes and using the answer above you can get the current user information with my answer. Hope you found the code easy to read and understandable.

    – OkiRules
    Mar 25 at 9:44











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329399%2fget-value-for-current-user-from-firebase-firestore-swift-4%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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()
])






share|improve this answer























  • This one worked, i didn't know that i have to pass the userid on the create action. Thanks a lot.

    – Leonardo Barroeta
    Mar 24 at 23:53















1














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()
])






share|improve this answer























  • This one worked, i didn't know that i have to pass the userid on the create action. Thanks a lot.

    – Leonardo Barroeta
    Mar 24 at 23:53













1












1








1







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()
])






share|improve this answer













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()
])







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 23:50









Sean StaynSean Stayn

1,78431024




1,78431024












  • This one worked, i didn't know that i have to pass the userid on the create action. Thanks a lot.

    – Leonardo Barroeta
    Mar 24 at 23:53

















  • This one worked, i didn't know that i have to pass the userid on the create action. Thanks a lot.

    – Leonardo Barroeta
    Mar 24 at 23:53
















This one worked, i didn't know that i have to pass the userid on the create action. Thanks a lot.

– Leonardo Barroeta
Mar 24 at 23:53





This one worked, i didn't know that i have to pass the userid on the create action. Thanks a lot.

– Leonardo Barroeta
Mar 24 at 23:53













0














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.






share|improve this answer























  • I've tried this before, and i have the same issue, it didn't work.

    – Leonardo Barroeta
    Mar 24 at 23:29











  • Can you please update your code with when a user signs up, and maybe also a photo of your database?

    – OkiRules
    Mar 24 at 23:31











  • Yes and using the answer above you can get the current user information with my answer. Hope you found the code easy to read and understandable.

    – OkiRules
    Mar 25 at 9:44















0














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.






share|improve this answer























  • I've tried this before, and i have the same issue, it didn't work.

    – Leonardo Barroeta
    Mar 24 at 23:29











  • Can you please update your code with when a user signs up, and maybe also a photo of your database?

    – OkiRules
    Mar 24 at 23:31











  • Yes and using the answer above you can get the current user information with my answer. Hope you found the code easy to read and understandable.

    – OkiRules
    Mar 25 at 9:44













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 23:23









OkiRulesOkiRules

19613




19613












  • I've tried this before, and i have the same issue, it didn't work.

    – Leonardo Barroeta
    Mar 24 at 23:29











  • Can you please update your code with when a user signs up, and maybe also a photo of your database?

    – OkiRules
    Mar 24 at 23:31











  • Yes and using the answer above you can get the current user information with my answer. Hope you found the code easy to read and understandable.

    – OkiRules
    Mar 25 at 9:44

















  • I've tried this before, and i have the same issue, it didn't work.

    – Leonardo Barroeta
    Mar 24 at 23:29











  • Can you please update your code with when a user signs up, and maybe also a photo of your database?

    – OkiRules
    Mar 24 at 23:31











  • Yes and using the answer above you can get the current user information with my answer. Hope you found the code easy to read and understandable.

    – OkiRules
    Mar 25 at 9:44
















I've tried this before, and i have the same issue, it didn't work.

– Leonardo Barroeta
Mar 24 at 23:29





I've tried this before, and i have the same issue, it didn't work.

– Leonardo Barroeta
Mar 24 at 23:29













Can you please update your code with when a user signs up, and maybe also a photo of your database?

– OkiRules
Mar 24 at 23:31





Can you please update your code with when a user signs up, and maybe also a photo of your database?

– OkiRules
Mar 24 at 23:31













Yes and using the answer above you can get the current user information with my answer. Hope you found the code easy to read and understandable.

– OkiRules
Mar 25 at 9:44





Yes and using the answer above you can get the current user information with my answer. Hope you found the code easy to read and understandable.

– OkiRules
Mar 25 at 9:44

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329399%2fget-value-for-current-user-from-firebase-firestore-swift-4%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해