SAFE-Stack / docs

https://safe-stack.github.io/docs/
MIT License
145 stars 69 forks source link

How do I set the return URI for the Azure AD login to the client port during the dev phase? #273

Closed mrakgr closed 1 year ago

mrakgr commented 1 year ago

I am covering the SAFE Stack as a part of my F# webdev series. In the latest installment I am trying to do a login page using Azure AD, but I am running into difficulties with the challenge request.

It is no problem to redirect the user to the Azure AD login using ChallengeAsync, but doing that through the Vite proxy server results in an incorrect redirect uri being passed in the url. Here is my setup on the server side.

let main args =
    let builder = WebApplication.CreateBuilder(args)
    builder.Services.AddGiraffe() |> ignore

    builder.Services
        .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
        |> ignore

    let app = builder.Build()

    app
        .UseAuthentication()
        .Use(Func<HttpContext,RequestDelegate,Task>(fun ctx next -> task {
            if ctx.User = null || ctx.User.Identity.IsAuthenticated = false then
                if app.Environment.IsDevelopment() then
                    return! ctx.ChallengeAsync(AuthenticationProperties(RedirectUri="http://localhost:8080"))
                else
                    return! ctx.ChallengeAsync()
            else
                return! next.Invoke(ctx)
        }))
        .UseGiraffe(webApp)

    app.Run()

    0 // Exit code

I really wish that return! ctx.ChallengeAsync(AuthenticationProperties(RedirectUri="http://localhost:8080")) did what I wanted, but it has no effect, and instead the redirect uri has the server 5000 port instead. I am not sure what I should do here.

mrakgr commented 1 year ago

I've also opened an issue on the Vite page just in case I can't get an answer here.