This commit is contained in:
Dominic Villemure
2024-07-04 20:47:07 -04:00
parent fb5f925976
commit 3558952634
9 changed files with 62 additions and 19 deletions

View File

@@ -12,7 +12,7 @@ public interface IIdentityService
Task<Result> UpdateCurrentUserBannerPictureUrlAsync(string url); Task<Result> UpdateCurrentUserBannerPictureUrlAsync(string url);
Task<Result> UpdateCurrentUserProfilePictureUrlAsync(string url); Task<Result> UpdateCurrentUserProfilePictureUrlAsync(string url);
Task<Result> UpdateCurrentUserWebsiteIconUrlAsync(string url); Task<Result> UpdateCurrentUserWebsiteIconUrlAsync(string url);
Task<Result<string>> UpdateCurrentUserAsync(UserModel userModel, string? portraitUrl); Task<Result<string>> UpdateCurrentUserAsync(UserModel userModel);
Task<IList<string>> GetCurrentUserRolesAsync(); Task<IList<string>> GetCurrentUserRolesAsync();
Task<UserModel?> FindUserByIdAsync(string id); Task<UserModel?> FindUserByIdAsync(string id);
Task<UserModel?> FindUserByEmailAsync(string email); Task<UserModel?> FindUserByEmailAsync(string email);

View File

@@ -44,7 +44,7 @@ public class UpdateCurrentUserCommandHandler(IApplicationDbContext context, IIde
var userModel = mapper.Map<UserModel>(request); var userModel = mapper.Map<UserModel>(request);
userModel.Id = identityUser.Id; userModel.Id = identityUser.Id;
var result = await identityService.UpdateCurrentUserAsync(userModel, request.PortraitUrl); var result = await identityService.UpdateCurrentUserAsync(userModel);
await context.SaveChangesAsync(cancellationToken); await context.SaveChangesAsync(cancellationToken);

View File

@@ -29,12 +29,14 @@ public class GetMinimalUserQueryHandler(
identityUser = await identityService.GetUserByUserNameAsync(request.UserName); identityUser = await identityService.GetUserByUserNameAsync(request.UserName);
} }
var user = new MinimalUserDto var user = new MinimalUserDto
{ {
FirstName = identityUser?.FirstName ?? string.Empty, FirstName = identityUser?.FirstName ?? string.Empty,
LastName = identityUser?.LastName ?? string.Empty, LastName = identityUser?.LastName ?? string.Empty,
UserName = identityUser?.UserName ?? string.Empty UserName = identityUser?.UserName ?? string.Empty,
SocialNetworks = identityUser?.SocialNetworks ?? new(),
ProfileColors = identityUser?.ProfileColors ?? new(),
StoredDataUrls = identityUser?.StoredDataUrls ?? new(),
}; };
return user; return user;

View File

@@ -1,3 +1,5 @@
using Hutopy.Application.Users.Models;
namespace Hutopy.Application.Users.Queries.GetMinimalUser; namespace Hutopy.Application.Users.Queries.GetMinimalUser;
public class MinimalUserDto public class MinimalUserDto
@@ -5,4 +7,8 @@ public class MinimalUserDto
public required string FirstName { get; init; } public required string FirstName { get; init; }
public required string LastName { get; init; } public required string LastName { get; init; }
public required string UserName { get; init; } = String.Empty; public required string UserName { get; init; } = String.Empty;
public SocialNetworksModel SocialNetworks { get; init; } = new();
public ProfileColorsModel ProfileColors { get; init; } = new();
public StoredDataUrlsModel StoredDataUrls { get; init; } = new();
} }

View File

@@ -1,4 +1,5 @@
using Hutopy.Application.Common.Models; using System.Linq;
using Hutopy.Application.Common.Models;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
namespace Hutopy.Infrastructure.Identity; namespace Hutopy.Infrastructure.Identity;

View File

@@ -41,10 +41,42 @@ public class IdentityService(
var userModel = new UserModel() var userModel = new UserModel()
{ {
Id = response.Id, Id = response.Id,
UserName = response.UserName, UserName = response.UserName ?? string.Empty,
FirstName = response.FirstName, FirstName = response.FirstName,
LastName = response.LastName, LastName = response.LastName,
Email = response.Email, Email = response.Email ?? string.Empty,
Occupation = response.Occupation,
PhoneNumber = response.PhoneNumber ?? string.Empty,
BirthDate = response.BirthDate,
Country = response.Country,
City = response.City,
Address = response.Address,
About = response.About,
Description = response.Description,
SocialNetworks = new SocialNetworksModel
{
FacebookUrl = response.SocialNetworks.FacebookUrl,
InstagramUrl = response.SocialNetworks.InstagramUrl,
XUrl = response.SocialNetworks.XUrl,
LinkedInUrl = response.SocialNetworks.LinkedInUrl,
TikTokUrl = response.SocialNetworks.TikTokUrl,
YoutubeUrl = response.SocialNetworks.YoutubeUrl,
RedditUrl = response.SocialNetworks.RedditUrl,
YourWebsiteUrl = response.SocialNetworks.YourWebsiteUrl,
},
ProfileColors = new ProfileColorsModel
{
BannerTop = response.ProfileColors.BannerTop,
BannerBottom = response.ProfileColors.BannerBottom,
Accent = response.ProfileColors.Accent,
Menu = response.ProfileColors.Menu
},
StoredDataUrls = new StoredDataUrlsModel
{
ProfilePictureUrl = response.StoredDataUrls.ProfilePictureUrl,
BannerPictureUrl = response.StoredDataUrls.BannerPictureUrl,
WebsiteIconUrl = response.StoredDataUrls.WebsiteIconUrl,
}
}; };
return userModel; return userModel;
@@ -88,7 +120,7 @@ public class IdentityService(
return result; return result;
} }
public async Task<Result<string>> UpdateCurrentUserAsync(UserModel userModel, string? portraitUrl) public async Task<Result<string>> UpdateCurrentUserAsync(UserModel userModel)
{ {
var applicationUser = await userManager.FindByIdAsync(userModel.Id); var applicationUser = await userManager.FindByIdAsync(userModel.Id);
@@ -104,7 +136,6 @@ public class IdentityService(
applicationUser.Address = userModel.Address; applicationUser.Address = userModel.Address;
applicationUser.About = userModel.About; applicationUser.About = userModel.About;
applicationUser.Description = userModel.Description; applicationUser.Description = userModel.Description;
applicationUser.PortraitUrl = portraitUrl;
applicationUser.SocialNetworks = new SocialNetworks applicationUser.SocialNetworks = new SocialNetworks
{ {
FacebookUrl = userModel.SocialNetworks.FacebookUrl, FacebookUrl = userModel.SocialNetworks.FacebookUrl,
@@ -155,7 +186,6 @@ public class IdentityService(
Address = response.Address, Address = response.Address,
About = response.About, About = response.About,
Description = response.Description, Description = response.Description,
PortraitUrl = response.PortraitUrl,
SocialNetworks = new SocialNetworksModel SocialNetworks = new SocialNetworksModel
{ {
FacebookUrl = response.SocialNetworks.FacebookUrl, FacebookUrl = response.SocialNetworks.FacebookUrl,
@@ -387,7 +417,7 @@ public class IdentityService(
email: user.Email, email: user.Email,
firstname: user.FirstName, firstname: user.FirstName,
lastname: user.LastName, lastname: user.LastName,
portraitUrl: user.PortraitUrl); profilePictureUrl: user.StoredDataUrls.ProfilePictureUrl);
return token; return token;
} }

View File

@@ -1,4 +1,6 @@
using Hutopy.Application.Common.Interfaces; using System;
using System.Threading.Tasks;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.Common.Models; using Hutopy.Application.Common.Models;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;

View File

@@ -1,4 +1,6 @@
using System.IdentityModel.Tokens.Jwt; using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
@@ -15,7 +17,7 @@ public static class JwtTokenHelper
string? email, string? email,
string? firstname, string? firstname,
string? lastname, string? lastname,
string? portraitUrl) string? profilePictureUrl)
{ {
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)); var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
@@ -31,9 +33,9 @@ public static class JwtTokenHelper
new Claim(ClaimTypes.Surname, lastname), new Claim(ClaimTypes.Surname, lastname),
}); });
if (portraitUrl is not null) if (profilePictureUrl is not null)
{ {
claims.Add(new Claim("portrait-url", portraitUrl)); claims.Add(new Claim("portrait-url", profilePictureUrl));
} }
var token = new JwtSecurityToken( var token = new JwtSecurityToken(

View File

@@ -71,7 +71,7 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto
user.Email, user.Email,
user.FirstName, user.FirstName,
user.LastName, user.LastName,
user.PortraitUrl); user.StoredDataUrls.ProfilePictureUrl);
return Ok(new { accessToken = token, email }); return Ok(new { accessToken = token, email });
} }