Skip to content

NOT A BUG Just a Question #2

@hansaliyad1

Description

@hansaliyad1

Hey!

I am following your Supabase implementation for my project, and it has been a great help since there is limited documentation on Supabase integration with C#.

I have a MAUI Blazor project for a Windows device, and I am trying to implement an auth flow similar to what you have in the HandleButtonClicked method of GithubLoginButton.razor.
I know WebAuthenticator is not supported on Windows devices, so I am using WinUIEx.WebAuthenticator. It partially works — it opens the browser, I can enter my username and password, and after authentication, it redirects to: https://localhost:7207/?error=invalid_request&error_code=bad_oauth_state&error_description=OAuth+callback+with+invalid+state.

I have been working on this since Thursday and have checked the redirectTo URI in Supabase and in my code at least 100 times. It is set to the correct custom URI, but it always redirects to https://localhost:7207/, which is the site URL for the Blazor app, with errors in the URL parameters.

I'm not sure what I’m missing, and I was wondering — if you have come across a similar issue and know the solution, it would be really helpful to me.

Your Code :

private async void HandleButtonClicked(MouseEventArgs obj)
    {
        var state = await Supabase.Auth.SignIn(Constants.Provider.Github, new SignInOptions
        {
            FlowType = Constants.OAuthFlowType.PKCE,
            RedirectTo = "supasharptodo://oauth/github/callback"
        });

        var authResult = await WebAuthenticator.Default.AuthenticateAsync(state.Uri, new Uri("supasharptodo://"));

        if (authResult.Properties.TryGetValue("code", out var code))
        {
            await Supabase.Auth.ExchangeCodeForSession(state.PKCEVerifier!, code);
        }
    }

My Code :

public async Task SignInWithAzureAsync()
{   
    try {
        // Define the redirect URI scheme consistently
        string redirectScheme = "com.app.dms://login-callback";
        var options = new SignInOptions
        {
            RedirectTo = redirectScheme,
            Scopes = "openid email profile offline_access user.read",
            FlowType = Constants.OAuthFlowType.PKCEVerifier
        };
        
        var authResponse = await _supabaseClient.Auth.SignIn(Constants.Provider.Azure, options);
        
        if (OperatingSystem.IsWindows())
        {
            // Use the SAME redirect URI as in options
            WinUIEx.WebAuthenticatorResult winAuthResult = await WinUIEx.WebAuthenticator.AuthenticateAsync(authResponse.Uri, new Uri(redirectScheme));
            if (winAuthResult != null && winAuthResult.Properties.Count > 0)
            {
                // Get the authorization code from the result
                string authCode = string.Empty;
                if (winAuthResult.Properties.TryGetValue("code", out string? code))
                {
                    authCode = code;
                }
                
                if (!string.IsNullOrEmpty(authCode))
                {
                    // Exchange the code for a session using both codeVerifier and authCode
                    await _supabaseClient.Auth.ExchangeCodeForSession(authResponse.PKCEVerifier!, authCode);
                }
            }
        }
        else
        {
            throw new Exception("Please login from Windows Device. This app only works on Windows device.");
        }
    }
    catch (Exception ex)
    {
        throw new Exception($"Azure login error: {ex.Message}");
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions