0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Reactを使ってブログサイトを作る 04 - トークン管理

Last updated at Posted at 2024-12-30

Reduxを使ったトークン管理

背景

  • Token(トークン)はユーザーのデータ識別子として、APIの権限管理において重要な役割を果たします。つまり、バックエンドでは多くのAPIが、現在のリクエストヘッダーにトークンデータが含まれているかどうかを確認し、正常にデータを返すかどうかを判断します。
  • Tokenはユーザーの識別データとして、多くのモジュールで共有する必要があります。Reduxを使用することで、状態の共有問題を簡単に解決できます。

実装方法

  1. Redux内で、トークンを取得する非同期処理と、トークンを変更する同期処理を実装します。
  2. Loginコンポーネントがactionを送信し、フォームデータを渡す役割を担います。

code

  • strore/modules/user.js
import { request } from '@/utils';
import {createSlice} from '@reduxjs/toolkit'
const userStore = createSlice({
name:'user',
initialState:{
token :''
    },
reducers:{  
setToken(state, action) {
state.token = action.payload
        }
    },
});
const  fetchLogin = (loginForm) =>{
return async (dispatch)=>{
const res = await request.post('/v1_0/authorizations',loginForm)
dispatch(setToken(res.data.token))
    }
}
const { setToken } = userStore.actions
const userReducer = userStore.reducer
export {setToken, fetchLogin}
export default userReducer;

  • store/index.js
import { configureStore } from '@reduxjs/toolkit'
import userReducer from './modules/user'
export default configureStore({
reducer: {
user: userReducer
  }
})

  • index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.scss';
import App from './App';
import { RouterProvider } from 'react-router-dom'
import router from './router'
import {Provider} from 'react-redux'
import store from './store'
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<Provider store = {store}>
<RouterProvider router={router} />
</Provider>
</React.StrictMode>
);

  • pages/Login/index.js
    image.png

以下是翻译成日文的内容:


トークンの永続化

課題

  • Reduxにトークンを保存した後、ブラウザをリロードするとトークンが失われます。(永続化とは、リロード時にトークンが失われないようにすることです)
  • Reduxはブラウザのメモリを利用したストレージ方式であるため、リロードすると状態が初期値に戻ります。
    image.png

解決策

  1. トークンを取得した後、ReduxとlocalStorageの両方に保存します。
  2. トークンを初期化する際、まずlocalStorageにトークンが存在するか確認し、存在する場合はlocalStorageに保存されているトークンを使用して値を設定します。
    image.png
    image.png

APIのモジュール化

  • トークンに関する各種操作は、プロジェクトの複数のモジュールで使用されるため、ツール関数としてカプセル化することで共有・再利用が可能です。

image.png

image.png
image.png
image.png

リクエストインターセプターによるトークンの注入

ルート認証

実装の考え方

  • ローカルにトークンがあるか確認し、ある場合は子コンポーネントを返し、なければログインページ(Login)にリダイレクトします。

実装手順

  • components ディレクトリに AuthRoute/index.jsx ファイルを作成します。
  • ログインしている場合、対応するページコンポーネントをレンダリングします。
  • ログインしていない場合、ログインページにリダイレクトします。
  • 認証が必要なページのルート設定を、AuthRoute コンポーネントを使ってレンダリングするように変更します。

image.png
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?