Learn Courses My Dashboard

Retrieve data from Firestore (CodeWithChris video: Custom iOS Login Page)

Hello, Im learning swift and programming in general and this is my first post here :slightly_smiling_face:

Yesterday I did it the Chris tutorial from youtube: Firebase Authentication Tutorial 2020 - Custom iOS Login Page (Swift) and works perfectly, now I can register and login users so I have the same code than him, so yesterday I ask him in the video this question but hi told me to ask here for help, my question is:

How I can retrieve the data of the user (name, mail,etc…) for a new screen like My account once is logged for example?

Thank you

Hi! some help with this please or should I ask in the students area? thank you! :slight_smile:

You can create a collection called users in which u store your user information as individual documents, with the id of the documents preferably being the id of ur user, so u can look up quickly.

1 Like

Thank you @Abdul_Wahid!

I know that i’m asking too much but taking in consideration that I’m using the same code that Chris for login/register, can you do an example of the code?

I’m doing already the CWC+ 90 days but I have to still learning

Thank you! :slight_smile:

I Haven’t watched that video so I don’t know exactly how Chris sets up the DB in that project.

However, I would assume that the general pattern would be the same.

Assuming that u have a collection called users with the docId of each user document being the uid of that particular user,

First u would need to have a model for ur user with the specific properties u need

struct User {
    var firstName: String
    var lastName: String
func getUserData() -> User {
        // Check there is a user
        guard let firebaseUser = Auth.auth().currentUser else { return }
        // Get metadata
        let path = db.collection("users").document(firebaseUser.uid)
        path.getDocument { snapshot, error in
            // Check there is a snapshot
            guard let snapshot = snapshot else { return }
            // Get the data and the user
            let data = snapshot.data()
            // Parse the data
            // Create a new user with the data

            // var createdUser = User()
            // return createdUser

(The implementation of this function is not complete)

This function would get a new user and return it to you. Note that I have commented out the part where u create the user, and the implementation for that is not complete as I don’t know how the code was structured, where ur holding the data, and where this function is called.

After this u have a user object which you can use to populate a view.

Hope this helps.

PS: If u copy and paste this code it will probably fail as I copied it from a different project I had and modified some parts of it. I just posted it here so u would have an idea of where to get started.

If ur still having trouble implementing it, then module 4 of the Swiftui database course takes u through a more in depth tutorial on implementing authentication.

1 Like

Oh thank you very much @Abdul_Wahid! I will still learning and check the module 4!

1 Like