Skip to content

Commit

Permalink
Merge pull request #26 from eliasson/improve-first-login
Browse files Browse the repository at this point in the history
Improve first login
  • Loading branch information
eliasson authored Oct 15, 2023
2 parents 24011a9 + 666d5bc commit 406da83
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/Quarter.Style/src/pages/_timesheet.sass
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
width: 20px
height: 20px

.q-activity-list--header
font-size: $font-size-kilo
font-weight: 600
margin-bottom: $pillow-small

.q-activity-list
display: none

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class="q-list">
<div class="q-list-item @(_isActive ? "q-list-item--expanded" : "")" >
<div class="q-list-item__header" @onclick="@ToggleActive">
<div class="q-activity-list--header">Current activity:</div>
<div class="q-activity-marker">
@{
var selectedActivity = SelectedActivity();
Expand Down
27 changes: 18 additions & 9 deletions src/Quarter/Services/UserAuthorizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Quarter.Auth;
using Quarter.Core.Auth;
using Quarter.Core.Commands;
using Quarter.Core.Exceptions;
using Quarter.Core.Models;
using Quarter.Core.Options;
Expand Down Expand Up @@ -73,27 +73,28 @@ public class UserAuthorizationService : IUserAuthorizationService
private readonly IUserRepository _userRepository;
private readonly AuthenticationStateProvider _authenticationStateProvider;
private readonly ILogger<UserAuthorizationService> _logger;
private readonly ICommandHandler _commandHandler;
private readonly IOptions<AuthOptions> _authOptions;

public UserAuthorizationService(
AuthenticationStateProvider authenticationStateProvider,
IRepositoryFactory repositoryFactory,
ILogger<UserAuthorizationService> logger,
IOptions<AuthOptions> authOptions)
ICommandHandler commandHandler,
IOptions<AuthOptions> authOptions,
ILogger<UserAuthorizationService> logger)
{
_userRepository = repositoryFactory.UserRepository();
_authenticationStateProvider = authenticationStateProvider;
_logger = logger;
_commandHandler = commandHandler;
_authOptions = authOptions;
}

public async Task<AuthorizedResult> AuthorizeOrCreateUserAsync(string email, CancellationToken ct)
{
try
{
var user = await _userRepository.GetUserByEmailAsync(email, ct);
_logger.LogInformation("Successfully authorized user {Email} at login", email);
return AuthorizedResult.AuthorizedWith(ClaimsForUser(user).ToArray());
return await tryWithExistingUser();
}
catch (NotFoundException)
{
Expand All @@ -104,10 +105,18 @@ public async Task<AuthorizedResult> AuthorizeOrCreateUserAsync(string email, Can
}

_logger.LogInformation("Unauthorized user {Email} tried to login, creating new user and granting access", email);
var user = User.StandardUser(new Email(email));
user = await _userRepository.CreateAsync(user, ct);
return AuthorizedResult.AuthorizedWith(ClaimsForUser(user).ToArray());

var command = new AddUserCommand(new Email(email), ArraySegment<UserRole>.Empty);

await _commandHandler.ExecuteAsync(command, OperationContext.None, ct);
return await tryWithExistingUser();
}

async Task<AuthorizedResult> tryWithExistingUser()
{
var user = await _userRepository.GetUserByEmailAsync(email, ct);
_logger.LogInformation("Successfully authorized user {Email} at login", email);
return AuthorizedResult.AuthorizedWith(ClaimsForUser(user).ToArray());
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Quarter/wwwroot/quarter.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 406da83

Please sign in to comment.