Skip to content

Commit 4023505

Browse files
committed
Updated user profile editing and service
Updated the URL for the user profile edit page and made changes to the `EditProfile.razor` page to allow email editing. The `UpdateProfile` method in `EditProfile.razor` is now public and updates the user profile using the `UserProfileService`. The `UserProfileService` has been refactored to remove the dependency on `HttpClient` and now uses the `AuthenticationStateProvider`. Two new methods, `GetProfileData` and `UpdateProfileData`, have been added to `UserProfileService` to handle user profile data. The `UserProfile` class has been updated to remove the `EmailVerified` property and add a `UserId` property.
1 parent 9844119 commit 4023505

File tree

4 files changed

+78
-41
lines changed

4 files changed

+78
-41
lines changed

WebApp/Components/Layout/UserMenu.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<span class="dropdown-button"><img role="presentation" src="icons/user.svg" /></span>
1313
<div class="dropdown-content">
1414
<a class="dropdown-item" href="user/orders">My orders</a>
15-
<a class="dropdown-item" href="user/userprofile">Edit profile</a>
15+
<a class="dropdown-item" href="user/editprofile">Edit profile</a>
1616
<form class="dropdown-item" method="post" action="user/logout" @formname="logout" @onsubmit="LogOutAsync">
1717
<AntiforgeryToken />
1818
<button type="submit">Log out</button>

WebApp/Components/Pages/User/UserProfile.razor renamed to WebApp/Components/Pages/User/EditProfile.razor

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@page "/user/userprofile"
1+
@page "/user/editprofile"
22
@attribute [Authorize]
33
@attribute [StreamRendering]
44
@using Microsoft.AspNetCore.Components.Authorization
@@ -22,13 +22,11 @@
2222
<div class="userProfile-item">
2323
<label for="lastName">Last Name: @userProfile.LastName</label>
2424
</div>
25-
<div class="userProfile-item">
26-
<label for="email">Email Verified: @userProfile.EmailVerified</label>
27-
</div>
2825
<div class="userProfile-item">
2926
<label for="email">Email: @userProfile.Email</label>
27+
<input type="email" id="email" @bind="@userProfile.Email" />
3028
</div>
31-
<button type="submit">Update Profile</button>
29+
<button type="submit" @onclick="UpdateProfile">Update Profile</button>
3230
@if (isUpdating)
3331
{
3432
<p>Updating...</p>
@@ -41,29 +39,25 @@
4139
</div>
4240

4341
@code {
42+
[CascadingParameter]
43+
public HttpContext? HttpContext { get; set; }
4444
private UserProfileService.UserProfile? userProfile;
4545
private bool isUpdating;
4646
private string updateMessage = "";
4747

4848
protected override async Task OnInitializedAsync()
4949
{
50-
userProfile = await ProfileService.GetUserProfileAsync();
50+
userProfile = await ProfileService.GetProfileData(HttpContext!);
5151
}
5252

53-
private async Task UpdateProfile()
53+
public async Task UpdateProfile()
5454
{
5555
if (userProfile is not null)
5656
{
5757
isUpdating = true;
5858
try
5959
{
60-
var message = await ProfileService.UpdateProfile(userProfile);
61-
if (message.StatusCode == System.Net.HttpStatusCode.OK)
62-
{
63-
updateMessage = "Profile updated successfully!";
64-
// Re-fetch the user profile to get the updated information
65-
userProfile = await ProfileService.GetUserProfileAsync();
66-
}
60+
userProfile = await ProfileService.UpdateProfileData(HttpContext!, userProfile);
6761
}
6862
catch (Exception)
6963
{
File renamed without changes.
Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,96 @@
11
namespace eShop.WebApp.Services;
22

33
using System.Security.Claims;
4-
using System.Text;
5-
using System.Text.Json;
4+
using Microsoft.AspNetCore.Authentication.Cookies;
5+
using Microsoft.AspNetCore.Authentication;
66
using Microsoft.AspNetCore.Components;
77
using Microsoft.AspNetCore.Components.Authorization;
88

9-
public class UserProfileService(HttpClient httpClient, AuthenticationStateProvider authenticationStateProvider)
9+
public class UserProfileService(AuthenticationStateProvider authenticationStateProvider)
1010
{
11-
private readonly string userProfileApiUrl = "/api/userProfile";
1211
[CascadingParameter]
1312
public HttpContext HttpContext { get; set; } = default!;
1413

14+
public Task<UserProfile> GetProfileData(HttpContext httpContext)
15+
{
16+
var user = httpContext.User;
17+
18+
return Task.FromResult(new UserProfile
19+
{
20+
UserId = ReadClaim(ClaimTypes.NameIdentifier, user),
21+
Name = ReadClaim("name", user),
22+
LastName = ReadClaim("last_name", user),
23+
Email = ReadClaim(ClaimTypes.Email, user)
24+
});
25+
}
26+
27+
public async Task<UserProfile> UpdateProfileData(HttpContext httpContext, UserProfile updatedProfile)
28+
{
29+
// Access the existing profile data
30+
var user = httpContext.User;
31+
var userId = ReadClaim(ClaimTypes.NameIdentifier, user);
32+
var userName = ReadClaim("name", user);
33+
var lastName = ReadClaim("last_name", user);
34+
var email = ReadClaim(ClaimTypes.Email, user);
35+
36+
// Update the profile data with the provided values
37+
if (updatedProfile.Name != null)
38+
{
39+
userName = updatedProfile.Name;
40+
}
41+
if (updatedProfile.LastName != null)
42+
{
43+
lastName = updatedProfile.LastName;
44+
}
45+
if (updatedProfile.Email != null)
46+
{
47+
email = updatedProfile.Email;
48+
}
49+
50+
// Perform further operations with the updated profile data
51+
// For example, you can save the updated profile data to a database
52+
53+
// Update the user claims with the updated profile data
54+
var identity = (ClaimsIdentity)user.Identity;
55+
identity.RemoveClaim(identity.FindFirst(ClaimTypes.Name));
56+
identity.AddClaim(new Claim(ClaimTypes.Name, userName ?? string.Empty));
57+
identity.RemoveClaim(identity.FindFirst(ClaimTypes.Surname));
58+
identity.AddClaim(new Claim(ClaimTypes.Surname, lastName ?? string.Empty));
59+
identity.RemoveClaim(identity.FindFirst(ClaimTypes.Email));
60+
identity.AddClaim(new Claim(ClaimTypes.Email, email ?? string.Empty));
61+
62+
// save the updated profile data to the database
63+
64+
// Update the authentication state
65+
await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user);
66+
67+
// You can also update the profile data in other external systems or APIs
68+
69+
// Optionally, you can return the updated profile data
70+
return new UserProfile
71+
{
72+
UserId = userId,
73+
Name = userName,
74+
LastName = lastName,
75+
Email = email
76+
};
77+
}
78+
1579
public async Task<UserProfile> GetUserProfileAsync()
1680
{
1781
var authState = await authenticationStateProvider.GetAuthenticationStateAsync();
18-
authState.User.Claims.ToList().ForEach(claim => Console.WriteLine($"{claim.Type}: {claim.Value}"));
1982
var user = authState.User;
2083

2184
return new UserProfile
2285
{
2386
Name = ReadClaim("name", user),
2487
LastName = ReadClaim("last_name", user),
2588
Email = ReadClaim("email", user),
26-
EmailVerified = ReadClaim("email_verified", user),
2789
};
2890
}
2991

3092
private static string? ReadClaim(string type, System.Security.Claims.ClaimsPrincipal? user) => user.FindFirst(x => x.Type == type)?.Value;
3193

32-
/*public Task<HttpResponseMessage> UpdateProfile(UserProfile userProfile)
33-
{
34-
*//*await httpClient.PutAsJsonAsync(userProfileApiUrl, userProfile);*//*
35-
httpClient.ToString();
36-
Console.WriteLine(userProfileApiUrl + userProfile);
37-
return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
38-
}*/
39-
40-
// Use the AccountController to update the user profile
41-
public Task<HttpResponseMessage> UpdateProfile(UserProfile userProfile)
42-
{
43-
var request = new HttpRequestMessage(HttpMethod.Put, userProfileApiUrl)
44-
{
45-
Content = new StringContent(JsonSerializer.Serialize(userProfile), Encoding.UTF8, "application/json")
46-
};
47-
48-
return httpClient.SendAsync(request);
49-
}
50-
5194
public async Task<string?> GetBuyerIdAsync()
5295
{
5396
var authState = await authenticationStateProvider.GetAuthenticationStateAsync();
@@ -70,6 +113,6 @@ public class UserProfile
70113
public string? Name { get; set; }
71114
public string? LastName { get; set; }
72115
public string? Email { get; set; }
73-
public string? EmailVerified { get; set; }
116+
public string? UserId { get; set; }
74117
}
75118
}

0 commit comments

Comments
 (0)