Skip to content

Commit

Permalink
fix merge errors
Browse files Browse the repository at this point in the history
  • Loading branch information
deleteLater committed Nov 17, 2023
1 parent 7b3e6b2 commit 3b5f47c
Show file tree
Hide file tree
Showing 7 changed files with 1,932 additions and 1,290 deletions.
50 changes: 42 additions & 8 deletions modules/back-end/src/Application/Identity/LoginByEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Domain.Organizations;
using Domain.Policies;
using Domain.Users;
using Domain.Workspaces;
using Microsoft.Extensions.Logging;

namespace Application.Identity;
Expand All @@ -18,45 +19,54 @@ public class LoginByEmailValidator : AbstractValidator<LoginByEmail>
public LoginByEmailValidator()
{
RuleFor(x => x.Email)
.NotEmpty().WithErrorCode(ErrorCodes.EmailIsRequired)
.EmailAddress().WithErrorCode(ErrorCodes.EmailIsInvalid);
.NotEmpty().WithErrorCode(ErrorCodes.Required("email"))
.EmailAddress().WithErrorCode(ErrorCodes.Invalid("email"));

RuleFor(x => x.Password)
.NotEmpty().WithErrorCode(ErrorCodes.PasswordIsRequired);
.NotEmpty().WithErrorCode(ErrorCodes.Required("password"));
}
}

public class LoginByEmailHandler : IRequestHandler<LoginByEmail, LoginResult>
{
private readonly IIdentityService _identityService;
private readonly IUserService _userService;
private readonly IWorkspaceService _workspaceService;
private readonly IOrganizationService _orgService;
private readonly ILogger<LoginByEmailHandler> _logger;

public LoginByEmailHandler(
IIdentityService identityService,
IUserService userService,
IWorkspaceService workspaceService,
IOrganizationService orgService,
ILogger<LoginByEmailHandler> logger)
{
_identityService = identityService;
_userService = userService;
_workspaceService = workspaceService;
_orgService = orgService;
_logger = logger;
}

public async Task<LoginResult> Handle(LoginByEmail request, CancellationToken cancellationToken)
{
var user = await _userService.FindByEmailAsync(request.Email);
_logger.LogInformation("user {Identity} login by password", request.Email);

Guid workspaceId;
var user = await _userService.FindOneAsync(x => x.Email == request.Email);
if (user == null)
{
// create workspace
workspaceId = await CreateWorkspaceAsync();

// create user
var registerResult = await _identityService.RegisterByEmailAsync(request.Email, request.Password, UserOrigin.Local);
var registerResult = await _identityService.RegisterByEmailAsync(workspaceId, request.Email, request.Password, UserOrigin.Local);
_logger.LogInformation("user {Identity} registered", request.Email);

// create organization for new user
var orgName = $"Playground - {request.Email}";
var organization = new Organization(orgName);
var organization = new Organization(workspaceId, orgName);
await _orgService.AddOneAsync(organization);

// set user as org owner
Expand All @@ -66,7 +76,31 @@ public async Task<LoginResult> Handle(LoginByEmail request, CancellationToken ca
return LoginResult.Ok(registerResult.Token);
}

_logger.LogInformation("user {Identity} login by password", request.Email);
return await _identityService.LoginByEmailAsync(request.Email, request.Password);
var workspaces = await _userService.GetWorkspacesAsync(request.Email);
if (!workspaces.Any())
{
// if there is no workspace associated with the user, create one
workspaceId = await CreateWorkspaceAsync();
}
else
{
workspaceId = workspaces.First().Id;
}

return await _identityService.LoginByEmailAsync(workspaceId, request.Email, request.Password);

async Task<Guid> CreateWorkspaceAsync()
{
var workspace = new Workspace
{
Name = "Default Workspace",
Key = "default-workspace",
License = null,
Sso = null
};
await _workspaceService.AddOneAsync(workspace);

return workspace.Id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public async Task LoginByEmail_Success()
var request = new LoginByEmail
{
Email = TestUser.Email,
Password = TestUser.RealPassword,
WorkspaceKey = TestWorkspace.Key
Password = TestUser.RealPassword
};
var response = await _app.PostAsync("/api/v1/identity/login-by-email", request, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Uri: http://localhost/api/v1/identity/login-by-email,
Content: {
Headers: {
Content-Length: 44,
Content-Length: 26,
Content-Type: application/json; charset=utf-8
},
Value:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Uri: http://localhost/api/v1/identity/login-by-email,
Content: {
Headers: {
Content-Length: 67,
Content-Length: 43,
Content-Type: application/json; charset=utf-8
},
Value:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class DoLoginComponent implements OnInit {
const { identity, password } = this.pwdLoginForm.value;
posthog.capture('Click Login', { property: 'value' });

this.identityService.loginByEmail(identity, password).subscribe(
this.identityService.loginByEmail(identity, password, '').subscribe(
response => this.handleResponse(response),
error => this.handleError(error)
)
Expand Down
Loading

0 comments on commit 3b5f47c

Please sign in to comment.