1

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 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

3 Comments

I'm facing the same problem as you : my GetRoles function (accepting post requests) is never called and I'm using the 'Standard' plan. Any ideas ?
Did you link the Azure Function to the Static Web App in Azure? This is configured in the SWA resource in APIs section - click on Link and use your Azure Function that has GetRoles function. Doc: learn.microsoft.com/en-us/azure/static-web-apps/…
finally I get it working. The problem was an exception inside my function which prevent it from returning any roles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.