react-oidc-context icon indicating copy to clipboard operation
react-oidc-context copied to clipboard

Unable to use cookie storage to store ID and Access Tokens

Open TejasRGitHub opened this issue 2 years ago • 3 comments

Hi,

I am trying to use react-oidc-context with my Idp and wanted to store the tokens in the Cookie Storage. I saw there are options in the oidcConfig with which I can set the userStore to cookie storage . I am using a similar oidcConfig file but the tokens are not getting stored in the cookie storage.

Does react-oidc-context support storing into cookie storage ?

// src/index.jsx
import React from "react";
import ReactDOM from "react-dom";
import { AuthProvider } from "react-oidc-context";
import App from "./App";

const oidcConfig = {
  // .... ,
  userStore: new WebStorageStateStore({ store: window.cookieStorage}),
  // ...
};

ReactDOM.render(
  <AuthProvider {...oidcConfig}>
    <App />
  </AuthProvider>,
  document.getElementById("app")
);

TejasRGitHub avatar Nov 04 '23 21:11 TejasRGitHub

The interface of cookieStorage seems different than the direct supported localStorage, thus you cannot use WebStorageStateStore, but you can implement your own WebStorageStateStore and pass that to userStore....

pamapa avatar Nov 06 '23 09:11 pamapa

Hi @pamapa , Thanks for the reply. Could you please point me to a reference where I can how I can implement a WebStorageStateStore with cookieStorage

TejasRGitHub avatar Nov 06 '23 15:11 TejasRGitHub

This library is using underneath the library oidc-client-ts. For WebStorageStateStore see here https://github.com/authts/oidc-client-ts/blob/main/src/WebStorageStateStore.ts. You should be able either to exetnd form it and "override" or completely implement your custom class with implementation all public functions of WebStorageStateStore. In order to pass it down to the settings you may need to cast it, like userStore: new CustomWebStorageStateStore(...) as WebStorageStateStore.

pamapa avatar Nov 06 '23 15:11 pamapa