Test: Google oauth
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
24
src/Infrastructure/Services/GoogleService.cs
Normal file
24
src/Infrastructure/Services/GoogleService.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user