Test: Google oauth

This commit is contained in:
Kamigen
2024-04-15 19:10:32 -04:00
parent b6e65b416d
commit bd2410a98e
12 changed files with 132 additions and 15 deletions

View File

@@ -6,4 +6,5 @@ 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;
}

View File

@@ -1,3 +1,4 @@
using Google.Apis.Oauth2.v2.Data;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.Common.Models;
using Microsoft.AspNetCore.Authorization;
@@ -30,6 +31,23 @@ public class IdentityService(
return (result.ToApplicationResult(), user.Id);
}
public async Task<(Result Result, string UserId)> CreateUserAsync(Userinfo userInfo)
{
var user = new ApplicationUser
{
UserName = userInfo.Name,
Email = userInfo.Email,
FirstName = userInfo.GivenName,
LastName = userInfo.FamilyName
};
var password = Guid.NewGuid().ToString("N")[..32];
var result = await userManager.CreateAsync(user, password);
return (result.ToApplicationResult(), user.Id);
}
public async Task<bool> IsInRoleAsync(string userId, string role)
{

View File

@@ -0,0 +1,24 @@
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Oauth2.v2;
using Google.Apis.Oauth2.v2.Data;
using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Infrastructure.Services;
public class GoogleService : IGoogleService
{
public async Task<Userinfo?> GetUserInfoAsync(string accessToken)
{
var user = GoogleCredential.FromAccessToken(accessToken);
var service = new Oauth2Service(
new BaseClientService.Initializer
{
HttpClientInitializer = user,
ApplicationName = "Hutopy"
});
return await service.Userinfo.Get().ExecuteAsync();
}
}

View File

@@ -1,15 +1,13 @@
using Hutopy.Domain.Interfaces;
using Google.Apis.Oauth2.v2.Data;
using Hutopy.Domain.Interfaces;
using Hutopy.Domain.Models;
using Hutopy.Infrastructure.Identity;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Identity;
namespace Hutopy.Infrastructure.Services;
public class UserService(UserManager<ApplicationUser> userManager) : IUserService
{
private readonly UserManager<ApplicationUser> _userManager = userManager;
public async Task CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
{
var applicationUser = new ApplicationUser
@@ -19,19 +17,36 @@ public class UserService(UserManager<ApplicationUser> userManager) : IUserServic
FirstName = firstName,
LastName = lastName
};
//todo: Need to handle errors better for the user.
var response = await _userManager.CreateAsync(applicationUser, password);
if (response.Errors.Any())
var response = await userManager.CreateAsync(applicationUser, password);
if (!response.Succeeded)
{
throw new InvalidOperationException(response.Errors.First().Description);
throw new Exception("Failed to create user");
}
}
public async Task CreateUserAsync(Userinfo userInfo)
{
var applicationUser = new ApplicationUser
{
UserName = userInfo.Name,
Email = userInfo.Email,
FirstName = userInfo.GivenName,
LastName = userInfo.FamilyName
};
var response = await userManager.CreateAsync(applicationUser, Guid.NewGuid().ToString("N")[..32]);
if (!response.Succeeded)
{
throw new Exception("Failed to create user");
}
}
public async Task<UserModel?> FindUserByIdAsync(string id)
{
var response = await _userManager.FindByIdAsync(id);
var response = await userManager.FindByIdAsync(id);
if (response == null) return null;
@@ -49,7 +64,7 @@ public class UserService(UserManager<ApplicationUser> userManager) : IUserServic
public async Task<UserModel?> FindUserByEmailAsync(string email)
{
var response = await _userManager.FindByEmailAsync(email);
var response = await userManager.FindByEmailAsync(email);
if (response == null) return null;