In this tutorial, we’ll focus on creating user profiles.
In this tutorial, we’ll focus on creating user profiles.
To begin, have your project opened in the browser from where we left off in the previous video and log out. Then open Visual Studio Code.
localhost:4200
Creating the Profile Component
Open the terminal and create the Profile Component. Run the ng generate component command and then place it in a directory call tools.
ng generate component tools/Profile
Go into the Profile Component TypeScript file, get the selector and add it at the bottom of the App Component HTML page.
Modifying the Profile Component
Next, set the styles for the input and textarea.
If we go to our app, we are almost there. We need to place the contents in the center and set when we want it to be visible.
Go to the Profile Component TypeScript file and add an input call show. This property will control when the component will be visible.
@Input() show: boolean;
Go to the Profile Component CSS file. In the profile selector, set display to flex, justify-content to center, and align-items: center to center the items.
Add a class call active and idle. For active, set the width to 100vw. For idle, set the width to 0.
Go to the Profile HTML page and add the ngClass directive to the profile div.
If we go back to the app, the profile page will be gone because we did not input a value for show.
Go to the App Component HTML page and pass in a value for the show input.
<app-profile [show]="loggedIn()"></app-profile>
If we run the app and log in, the profile page will show.
Setting up Firestore
Lets add the function to upload the data into Firebase.
For test, reading and writing data is only allowed up to a specified date.
Select production mode and click next. For the location, leave it at default and click Enable.
The Data tab is where we see all the data in our database. If we select the rules tab, you will see that reading and writing data is prohibited. Change false to true and publish it.
Uploading user profiles
Go into the Profile HTML page and add a template reference for the input and text area. Then attach a click event to the button and pass in the 2 references.
Go into the Profile Component TypeScript file and define the function.
Import the FirebaseTSFirestore and the FirebaseTSAuth classes and create an object for each.
Inside the onContinueClick() function, get the value of the inputs.
How data are stored in Firestore
We can have multiple documents in a collection and we can have multiple collections in our database.
path: [ "Users", this.auth.getAuth().currentUser.uid ]
For the data, each user will have a publicName and description.
In the onComplete callback function, print a message and reset the inputs.
If we go back to our app on the browser and fill in the information, we should get a pop up that says we created the profile.
Retrieving user profile
Define a function call getUserProfile().
If we look at our app, even though we have a profile for our user, the profile component is still showing. To fix this, go to the app component TypeScript file. Declare a Boolean call userHasProfile and set it to true.
userHasProfile = true;
In the getUserProfile() function, set userHasProfile to result.exists and then call the function inside the whenSignedInAndEmailVerified callback function.
Go to the App Component HTML page and locate the show input for the Profile component. We want to show the component only if we are logged in and do not have a profile.
<app-profile [show]="loggedIn() && !userHasProfile"></app-profile>
If we go to the app, the profile creator should not show since we have a profile already. If we delete the data from our database, the profile creator will show up since we no longer have a profile.
Displaying the User Profile Data
Lets see how we can display the data.
userDocument: any;
To get the data, grab the result object, and call the data() method. Then pass it into the userDocument variable.
result.data() will return a JSON object with the document’s data.
this.userDocument.publicName;
As best practice, we should define an interface for our documents so we know what properties it has. Right now, when we grab the document object and try to access the properties, nothing is showing up. To change this, define an interface call UserDocument and add its properties.
Make sure that the names of each property matches the ones in the database.
Once we have the interface for the document, set the type of the userDocument variable to UserDocument. Then locate the getUserProfile function and cast the data to UserDocument.
userDocument: UserDocument ;
Lastly, go to the App Component CSS file and add a right margin to the nav-bar-items so they will be separated.
If we go to the app, we should see our name in the navbar.