7.10: Displaying Profile Photo in the NavBar
The next to modules are optional and are really a design decision. However, it is also a really good learning opportunity for passing data between components that isn't Parent -> Child
or Child -> Parent
.
Return UserForList DTO on Login
In our .API project - in the AuthService.cs
file, let's modify the login method to make sure we're returning photos with the user:
Now let's open up our old friend the AuthController.cs
file.
We're going to map the user we're retrieving from the database to a UserForList DTO. We don't want to send back a full user because it has too much information.
Just before the return statement in the Login()
method, add the following mapping and update the object in the return statent to also return a user:
Updating the AuthService in Angular
Add a property called currentUser
, add the user item to local storage, and populate the currentUser property:
In order to load this information when the page loads, let's also update our root app.component.ts
file:
When we logout - we also need to make sure that the user item from local storage is removed along with the token:
Open up nav.component.ts
file and add the following:
Adding Profile Photo in the NavBar
In nav.component.html
, edit the dropdown <div>
to the following:
We'll also add some styling in nav.component.scss
:
Now, our profile page is displayed next to the dropdown when we're logged in. But, we're faced with a similar problem: if we change our profile picture - it is updated in the profile tab of our edit profile component - but, the change is not reflected in our navbar.
This is a little trickier since the NavBar is not a parent or a child component of our Photo Editor component. We'll see how to do it next.
Last updated