In Blazor app, how do I access the JWT from Google sign-in and set the verified user?

22 Views Asked by At

I'm new to Blazor and I'm trying to use Google sign-in from a server-side app. I've gotten to the point where I can execute the sign-in and handle the post request from Google. Here's the class that maps the post request to a method:

public static class ApiExtensions
{
    public static IEndpointRouteBuilder MapApiExtensions(this IEndpointRouteBuilder builder)
    {
        builder.MapPost("google-signin", static () =>
        {
            Console.WriteLine("In post");
            return Results.Redirect("/");
        });

        return builder;
    }
}

I have a component that looks for a User claims principal in AuthenticationStateProvider:

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        try
        {
            _user = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User;

            // Try to get the GivenName
            var givenName = _user.FindFirst(ClaimTypes.GivenName);
            _givenName = givenName != null ? givenName.Value : _user.Identity?.Name ?? "Unknown";

            var surname = _user.FindFirst(ClaimTypes.Surname);
            _surname = surname != null ? surname.Value : _user.Identity?.Name ?? "Unknown";

            var avatar = _user.FindFirst("urn:google:image");
            _avatar = avatar != null ? avatar.Value : "";
        }
        catch (Exception e)
        {
            Logger.LogError(e, "Failed to resolve the authenticated user");
        }
    }

How do I access the JWT from the post request and get the user set up in the authentication state?
0

There are 0 best solutions below