11namespace eShop . WebApp . Services ;
22
33using System . Security . Claims ;
4- using System . Text ;
5- using System . Text . Json ;
4+ using Microsoft . AspNetCore . Authentication . Cookies ;
5+ using Microsoft . AspNetCore . Authentication ;
66using Microsoft . AspNetCore . Components ;
77using 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