Home > Net >  How is this access token stored on the client, in FastAPI's tutorial "Simple OAuth2 with P
How is this access token stored on the client, in FastAPI's tutorial "Simple OAuth2 with P

Time:11-06

I'm pretty new to FastAPI and OAuth2 in general. I just worked through the tutorial enter image description here

I click Authorize and enter my credentials. (username: johndoe, password: secret) enter image description here

And now I can access the /users/me endpoint.

enter image description here

Notice how the header Authorization: Bearer johndoe was automagically included in my request.

Last notes:

  1. I've checked my cookies, session storage, and local storage and all are empty
  2. The authorization header disappears if I refresh the page or open a new tab

I suspect Swagger is doing something under the hood here, but I can't put my finger on it.

CodePudding user response:

If you need persistence for the token you'd usually use localStorage or similar, but in SwaggerUIs specific case, the authentication information is kept internally in the library.

If you have enabled persistence SwaggerUI will persistent the access token to localStorage:

export const persistAuthorizationIfNeeded = () => ( { authSelectors, getConfigs } ) => {
  const configs = getConfigs()
  if (configs.persistAuthorization)
  {
    const authorized = authSelectors.authorized()
    localStorage.setItem("authorized", JSON.stringify(authorized.toJS()))
  }
}
  • Related