How to debug Custom Authentication (GetRoles api) in Azure Static Web App?

49 views Asked by At

According to the documentation of Static Web App (SWA), you can create an Azure Function that will allow to assign an authenticated User custom roles, usually called GetRoles: SWA custom auth. The problem is that I am not able to get this functionality to work - even though I added the function it does not get triggered and I can not assign custom roles to the User.

I have used SWA cli to debug locally with the provided authentication emulator, but when I get logged in, it does not trigger the GetRoles function, so I can not debug code that I am suppose to write. I have run the function in a separate terminal (to be able to attach in vsc) and started the SWA with: swa start ./my-dist --api-devserver-url http://localhost:7071.

I have also tried to deploy to Azure to see, if maybe there it will call GetRoles automatically, but that also did not work (Roles were still authenticated and anonymous).Here additional question appeared - how to see logs from the provided api function that was included in the SWA? It would have to be an dedicated Azure Function that was deployed separately (bring your own custom function)?

I have followed this example provided on GH: https://github.com/staticwebdev/roles-function, and just changed the function to return an array of some custom roles: ["admin", "reader"], so I have expected that now, after login, the roles would be: admin, reader, but it remains: authenticated, anonymous. What am I doing wrong here?

1

There are 1 answers

0
noro5 On

I eventually got to the point where I was able to debug and implement the functionality that I needed, but the solution is very cumbersome so still looking for better solutions. In summary:

  • I think the emulator is not triggering the GetRoles function so I was not able to check even an mocked payload. From what I saw it is not possible to configure it locally with an external solution like Active Directory.
  • The reason why the custom authorization function was not called on Azure was because I had wrong sku for my SWA which was free and it should be Standard. It is mentioned in the documentation, so my bad. After that, it triggered the function and I was able to assign some dummy roles that got assigned to the User after login.
  • In order to be able to debug the payload that I got from the Identity Provider (AD), I have deployed an Azure Function and linked it to the SWA. It got triggered as well and because there is a possibility to establish a remote debugging session in Visual Studio, I could connect and debug to see the payload.