Just cleanup
This commit is contained in:
@@ -13,7 +13,8 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto
|
||||
[HttpPost("/api/google/sign-in")]
|
||||
public async Task<IActionResult> SignIn([FromBody] GoogleSignInRequest request)
|
||||
{
|
||||
var httpClient = httpClientFactory.CreateClient();
|
||||
using var httpClient = httpClientFactory.CreateClient();
|
||||
|
||||
// Verify the token with Google
|
||||
var response = await httpClient.GetAsync($"https://www.googleapis.com/oauth2/v1/userinfo?access_token={request.AccessToken}");
|
||||
if (!response.IsSuccessStatusCode)
|
||||
@@ -21,12 +22,11 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto
|
||||
return BadRequest("Invalid Google token.");
|
||||
}
|
||||
|
||||
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
|
||||
|
||||
var email = payload["email"]?.ToString() ?? "";
|
||||
var name = payload["name"]?.ToString() ?? "";
|
||||
var givenName = payload["given_name"]?.ToString() ?? "";
|
||||
var familyName = payload["family_name"]?.ToString() ?? "";
|
||||
var userInfo = JObject.Parse(await response.Content.ReadAsStringAsync());
|
||||
var email = userInfo["email"]?.ToString() ?? "";
|
||||
var name = userInfo["name"]?.ToString() ?? "";
|
||||
var givenName = userInfo["given_name"]?.ToString() ?? "";
|
||||
var familyName = userInfo["family_name"]?.ToString() ?? "";
|
||||
|
||||
if (string.IsNullOrEmpty(email))
|
||||
{
|
||||
@@ -47,27 +47,29 @@ public class GoogleController(IIdentityService identityService, IHttpClientFacto
|
||||
}
|
||||
|
||||
// Sign in the user
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.Name, name),
|
||||
new(ClaimTypes.Email, email),
|
||||
new(ClaimTypes.GivenName, givenName),
|
||||
new(ClaimTypes.Surname, familyName)
|
||||
};
|
||||
|
||||
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));
|
||||
|
||||
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);
|
||||
var claimsIdentity = new ClaimsIdentity(
|
||||
new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.Name, name),
|
||||
new(ClaimTypes.Email, email),
|
||||
new(ClaimTypes.GivenName, givenName),
|
||||
new(ClaimTypes.Surname, familyName)
|
||||
},
|
||||
CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
|
||||
return Ok(new { accessToken = jwtToken, email });
|
||||
await HttpContext.SignInAsync(
|
||||
CookieAuthenticationDefaults.AuthenticationScheme,
|
||||
new ClaimsPrincipal(claimsIdentity));
|
||||
|
||||
var jwtSection = configuration.GetRequiredSection("Authentication:Jwt");
|
||||
|
||||
var token = JwtTokenHelper.GenerateJwtToken(
|
||||
issuer: jwtSection["Issuer"] ?? throw new ArgumentNullException("The Jwt issuer is missing."),
|
||||
audience: jwtSection["Audience"] ?? throw new ArgumentNullException("The Jwt audience is missing."),
|
||||
key: jwtSection["Key"] ?? throw new ArgumentNullException("The Jwt key is missing."),
|
||||
userId: user.Id);
|
||||
|
||||
return Ok(new { accessToken = token, email });
|
||||
}
|
||||
|
||||
public class GoogleSignInRequest
|
||||
|
||||
Reference in New Issue
Block a user