diff --git a/src/Infrastructure/Identity/ApplicationUser.cs b/src/Infrastructure/Identity/ApplicationUser.cs index 39cb433..0462aff 100644 --- a/src/Infrastructure/Identity/ApplicationUser.cs +++ b/src/Infrastructure/Identity/ApplicationUser.cs @@ -6,5 +6,4 @@ public class ApplicationUser : IdentityUser { public string FirstName { get; set; } = string.Empty; public string LastName { get; set; } = string.Empty; - //public string Gender { get; set; } = string.Empty; } diff --git a/src/Web/Controllers/GoogleController.cs b/src/Web/Controllers/GoogleController.cs index 1f5228e..49c57e2 100644 --- a/src/Web/Controllers/GoogleController.cs +++ b/src/Web/Controllers/GoogleController.cs @@ -8,7 +8,7 @@ using Newtonsoft.Json.Linq; namespace Hutopy.Web.Controllers; -public class GoogleController(IIdentityService identityService, IHttpClientFactory httpClientFactory) : Controller +public class GoogleController(IIdentityService identityService, IHttpClientFactory httpClientFactory, IConfiguration configuration) : Controller { [HttpPost("/api/google/sign-in")] public async Task SignIn([FromBody] GoogleSignInRequest request) @@ -41,7 +41,7 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto user = await identityService.FindUserByEmailAsync(email); } - if (user is null) + if (user?.Id is null) { return BadRequest("Unable to find or create the user."); } @@ -58,7 +58,14 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); - var jwtToken = JwtTokenHelper.GenerateJwtToken("https://hutopy.com", "Hutopy", "V3J3bWFuUml3ZVpQbmxlWmZhWEo3ZkJSZ01YbHBwS24=", user.Id!); + var issuer = configuration["Jwt:Issuer"] ?? + throw new ArgumentNullException("The Jwt issuer is missing."); + var audience = configuration["Jwt:Audience"] ?? + throw new ArgumentNullException("The Jwt audience is missing."); + var key = configuration["Jwt:Key"] ?? + throw new ArgumentNullException("The Jwt key is missing."); + + var jwtToken = JwtTokenHelper.GenerateJwtToken(issuer, audience, key, user.Id); return Ok(new { accessToken = jwtToken, email }); } diff --git a/src/Web/DependencyInjection.cs b/src/Web/DependencyInjection.cs index a921805..402b870 100644 --- a/src/Web/DependencyInjection.cs +++ b/src/Web/DependencyInjection.cs @@ -93,13 +93,16 @@ public static class DependencyInjection ValidateAudience = true, ValidAudience = configuration["Jwt:Audience"], ValidateLifetime = true, - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"] ?? "")) + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"] ?? + throw new ArgumentNullException("The Jwt Key is missing."))) }; }) .AddGoogle(GoogleDefaults.AuthenticationScheme, options => { - options.ClientId = configuration["Google:ClientId"] ?? ""; - options.ClientSecret = configuration["Google:ClientSecret"] ?? ""; + options.ClientId = configuration["Google:ClientId"] ?? + throw new ArgumentNullException("The Google ClientId is missing.");; + options.ClientSecret = configuration["Google:ClientSecret"] ?? + throw new ArgumentNullException("The Google ClientSecret is missing.");; }) .AddFacebook(FacebookDefaults.AuthenticationScheme, options => { diff --git a/src/Web/Program.cs b/src/Web/Program.cs index 1246d0d..781bdc2 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -61,8 +61,6 @@ app.UseCors("AllowHutopyUiPreview"); app.UseAuthentication(); app.UseAuthorization(); - - // Initialize and seed the db. await app.InitialiseDatabaseAsync();