Automatically set token in header
From my understanding in "normal" .net authentication flow the back-end authenticates user based on the cookies whereas with kinde i have to:
1. GetToken()
2. Set token in header
Authorization: Bearer token
Am i correct or am i using a bad authentication flow?
Regardless i have a few pages where i need to instantly make a few authenticated API requests. If I try to retrieve the token getToken()
instantly, i'll get an error message:
However if i refresh the page on a page without any instant authroized api requests and then navigate to the page with the code:
then it will work. So my questions are:
- how would i go about instant authorized api requests?
- Is there a way around having to call getToken()
and instead relying on cookie based authentication/authroization?3 Replies
Hi @LIFE,
I have passed these questions onto my .NET expert teammate.
I will get back to you on your .NET questions soon.
Hey @LIFE,
You're correct in your understanding of the Kinde authentication flow. The
getToken()
function is used to securely call your API and pass the bearer token to validate that your user is authenticated. This token is then set in the header as Authorization: Bearer token
.
As for the error you're encountering, it seems like the getToken()
function is being called before the Kinde authentication client is fully initialized. This is why it works when you navigate from a page without instant authorized API requests to a page with such requests - the client has had time to initialize.
To handle instant authorized API requests, you could use a loading state to ensure the Kinde client is ready before making the API call. Here's an example:
cs
Regarding your second question, Kinde's authentication flow is based on JWT tokens and not cookies. This is a design choice for security reasons. JWT tokens are stateless, meaning the server does not need to keep a record of tokens for validation, and they can be easily used across different domains, which is not always possible with cookies due to same-origin policies.
I hope this helps! Let me know if you have any other questions.That is great,
isLoading
was the missing key!
Appreciate the explanation of the authentication flow🫶No worries at all, glad to hear your problem is solved.