< Summary

Information
Class: Chronicis.Client.Services.AuthService
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/AuthService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 82
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 50
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetCurrentUserAsync()0%2352480%
IsAuthenticatedAsync()0%620%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/AuthService.cs

#LineLine coverage
 1using System.Security.Claims;
 2using Microsoft.AspNetCore.Components.Authorization;
 3
 4namespace Chronicis.Client.Services;
 5
 6/// <summary>
 7/// User information retrieved from Auth0
 8/// </summary>
 9public class UserInfo
 10{
 11    public string Auth0UserId { get; set; } = string.Empty;
 12    public string Email { get; set; } = string.Empty;
 13    public string DisplayName { get; set; } = string.Empty;
 14    public string? AvatarUrl { get; set; }
 15}
 16
 17/// <summary>
 18/// Implementation of authentication service using ASP.NET Core authentication
 19/// </summary>
 20public class AuthService : IAuthService
 21{
 22    private readonly AuthenticationStateProvider _authenticationStateProvider;
 23    private UserInfo? _cachedUser;
 24
 025    public AuthService(AuthenticationStateProvider authenticationStateProvider)
 26    {
 027        _authenticationStateProvider = authenticationStateProvider;
 028    }
 29
 30    public async Task<UserInfo?> GetCurrentUserAsync()
 31    {
 032        if (_cachedUser != null)
 33        {
 034            return _cachedUser;
 35        }
 36
 037        var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
 038        var user = authState.User;
 39
 040        if (!user.Identity?.IsAuthenticated ?? true)
 41        {
 042            return null;
 43        }
 44
 45        const string customNamespace = "https://chronicis.app";
 46
 47        // Extract claims from Auth0 token
 048        var auth0UserId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value
 049                         ?? user.FindFirst("sub")?.Value
 050                         ?? "";
 51
 052        var email = user.FindFirst($"{customNamespace}/email")?.Value
 053                   ?? user.FindFirst(ClaimTypes.Email)?.Value
 054                   ?? user.FindFirst("email")?.Value
 055                   ?? "";
 56
 057        var displayName = user.FindFirst($"{customNamespace}/name")?.Value
 058                         ?? user.FindFirst(ClaimTypes.Name)?.Value
 059                         ?? user.FindFirst("name")?.Value
 060                         ?? user.FindFirst("preferred_username")?.Value
 061                         ?? "Unknown User";
 62
 063        var avatarUrl = user.FindFirst($"{customNamespace}/picture")?.Value
 064                       ?? user.FindFirst("picture")?.Value;
 65
 066        _cachedUser = new UserInfo
 067        {
 068            Auth0UserId = auth0UserId,
 069            Email = email,
 070            DisplayName = displayName,
 071            AvatarUrl = avatarUrl
 072        };
 73
 074        return _cachedUser;
 075    }
 76
 77    public async Task<bool> IsAuthenticatedAsync()
 78    {
 079        var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
 080        return authState.User.Identity?.IsAuthenticated ?? false;
 081    }
 82}