Search code examples
javascriptoauth-2.0azure-ad-msal

How to deal with oauth cookie for api requests


My frontend cant seem to get login credentials. When going to routes on my backend, everything works fine. When trying to fetch data, I get an error indicating that there's no token on my backend.

I am using MSAL for Oauth on my backend. If I go to localhost:5000(backend) and hit a route, everything works fine. The auth works, I can see token information and everything.

My app at the moment redirects to Microsoft oauth page, then redirects me back to my frontend. I can see a cookie in my frontend with name 'session' and a value.

Now comes my problem. I dont know if I need to do something with this cookie? When reading the docs, it shows the auth flow containing the cookie to make api requests. But it doesn't say how to use the cookie.

When trying to get the cookie with document.cookie, it returns an empty string. Is this something I cant read with JavaScript but can still send with a request?


Solution

  • I just never worked with cookies before so answer may seem easy to others but it was:

    {credentials: 'include'} needed to be added to requests from frontend. Then backend needed Cors module with CORS(app,supports_credentials=True)