#oauth cleanup

This commit is contained in:
Dominic Villemure
2024-06-09 23:53:43 -04:00
parent 6f76cb2084
commit 66bfeea3ee
4 changed files with 16 additions and 9 deletions

View File

@@ -6,5 +6,4 @@ public class ApplicationUser : IdentityUser
{ {
public string FirstName { get; set; } = string.Empty; public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty; public string LastName { get; set; } = string.Empty;
//public string Gender { get; set; } = string.Empty;
} }

View File

@@ -8,7 +8,7 @@ using Newtonsoft.Json.Linq;
namespace Hutopy.Web.Controllers; 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")] [HttpPost("/api/google/sign-in")]
public async Task<IActionResult> SignIn([FromBody] GoogleSignInRequest request) public async Task<IActionResult> SignIn([FromBody] GoogleSignInRequest request)
@@ -41,7 +41,7 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto
user = await identityService.FindUserByEmailAsync(email); user = await identityService.FindUserByEmailAsync(email);
} }
if (user is null) if (user?.Id is null)
{ {
return BadRequest("Unable to find or create the user."); 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); var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); 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 }); return Ok(new { accessToken = jwtToken, email });
} }

View File

@@ -93,13 +93,16 @@ public static class DependencyInjection
ValidateAudience = true, ValidateAudience = true,
ValidAudience = configuration["Jwt:Audience"], ValidAudience = configuration["Jwt:Audience"],
ValidateLifetime = true, 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 => .AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
{ {
options.ClientId = configuration["Google:ClientId"] ?? ""; options.ClientId = configuration["Google:ClientId"] ??
options.ClientSecret = configuration["Google:ClientSecret"] ?? ""; 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 => .AddFacebook(FacebookDefaults.AuthenticationScheme, options =>
{ {

View File

@@ -61,8 +61,6 @@ app.UseCors("AllowHutopyUiPreview");
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
// Initialize and seed the db. // Initialize and seed the db.
await app.InitialiseDatabaseAsync(); await app.InitialiseDatabaseAsync();