トークンをLocalStorageに保存する – 【Auth0】

トークンをLocalStorageに保存する – 【Auth0】

ライブラリ

ライブラリバージョン
@auth0/auth0-spa-js2.0.8

LocalStorageに保存

Auth0では、トークンはdefaultではブラウザのin-memory方式を採用しています。※推奨されている

auth0Client = await auth0.createAuth0Client({
  domain: config.domain,
  clientId: config.clientId
});

cacheLocationを省略すると、デフォルトでmemoryを指定したことになります。

明示的にlocalstorageにするとLocalStorageに保存されるようになります。

auth0Client = await auth0.createAuth0Client({
  domain: config.domain,
  clientId: config.clientId,
  cacheLocation: 'localstorage'
});

確認

LocalStorageに保存されたかどうかはブラウザのデベロッパーツールで確認ができます。

ログイン成功するとLocalStorageに保存されているのがわかります。

参考サイト

Token Storage
Learnhowandwheretostoretokensusedintoken-basedauthentication.
Storing Auth0 tokens in session storage instead of local storage
HiTeam,Iamusingauth0-spa-jsinReactJS.IamusinggettokensilentlymethodtogetthetokenfromAuth0serverandcurrentlysettingthecachelocationtolocalstorage.Isthereanywayto...

コメント

タイトルとURLをコピーしました