< Summary - Syki

Information
Class: Syki.Front.Auth.SykiAuthStateProvider
Assembly: Front
File(s): /home/runner/work/syki/syki/Front/Auth/SykiAuthStateProvider.cs
Tag: 4_16869239191
Line coverage
82%
Covered lines: 14
Uncovered lines: 3
Coverable lines: 17
Total lines: 43
Line coverage: 82.3%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetAuthenticationStateAsync()50%2280%
MarkUserAsAuthenticated()100%11100%
MarkUserAsLoggedOut()100%210%
CreateClaimsPrincipalFromToken(...)100%11100%

File(s)

/home/runner/work/syki/syki/Front/Auth/SykiAuthStateProvider.cs

#LineLine coverage
 1using Microsoft.JSInterop;
 2using System.Security.Claims;
 3using Microsoft.AspNetCore.Components.Authorization;
 4
 5namespace Syki.Front.Auth;
 6
 9127public class SykiAuthStateProvider(ILocalStorageService storage) : AuthenticationStateProvider
 8{
 9    public override async Task<AuthenticationState> GetAuthenticationStateAsync()
 10    {
 88411        var user = await storage.GetItemAsync<GetUserAccountOut>("User");
 12
 88413        if (user == null)
 14        {
 015            return new(new ClaimsPrincipal(new ClaimsIdentity()));
 16        }
 17
 88418        return new(CreateClaimsPrincipalFromToken(user));
 88419    }
 20
 21    public void MarkUserAsAuthenticated()
 22    {
 88423        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
 88424    }
 25
 26    public void MarkUserAsLoggedOut()
 27    {
 028        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
 029    }
 30
 31    private static ClaimsPrincipal CreateClaimsPrincipalFromToken(GetUserAccountOut user)
 32    {
 88433        var identity = new ClaimsIdentity("Bearer");
 34
 88435        identity.AddClaim(new Claim("sub", user.Id.ToString()));
 88436        identity.AddClaim(new Claim("name", user.Name));
 88437        identity.AddClaim(new Claim("email", user.Email));
 88438        identity.AddClaim(new Claim("role", user.Role.ToString()));
 88439        identity.AddClaim(new Claim(ClaimTypes.Role, user.Role.ToString()));
 40
 88441        return new(identity);
 42    }
 43}