## 概要 [Task2910: 画面実装(トークンを定期的に更新する仕組み)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2910) - タイマーで定期的に代行操作用のアクセストークンを更新する処理を実装しました。 ## レビューポイント - 通常のアクセストークンのタイマー内で同じタイミングでチェックするように実装していますが分けたほうがいいなどありますでしょうか? ## UIの変更 - [Task2910](https://ndstokyo.sharepoint.com/:f:/r/sites/Piranha/Shared%20Documents/General/OMDS/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88/Task2910?csf=1&web=1&e=g0RdIf) ## 動作確認状況 - ローカルで確認
82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
|
import {
|
|
removeAccessToken,
|
|
initialConfig,
|
|
loadAccessToken,
|
|
saveAccessToken,
|
|
loadRefreshToken,
|
|
saveRefreshToken,
|
|
removeRefreshToken,
|
|
} from "./utils";
|
|
import type { AuthState } from "./state";
|
|
import {
|
|
getDelegationTokenAsync,
|
|
updateDelegationTokenAsync,
|
|
updateTokenAsync,
|
|
} from "./operations";
|
|
|
|
const initialState: AuthState = {
|
|
configuration: initialConfig(),
|
|
accessToken: loadAccessToken(),
|
|
refreshToken: loadRefreshToken(),
|
|
delegationAccessToken: null,
|
|
delegationRefreshToken: null,
|
|
};
|
|
|
|
export const authSlice = createSlice({
|
|
name: "auth",
|
|
initialState,
|
|
reducers: {
|
|
setToken: (
|
|
state,
|
|
action: PayloadAction<{
|
|
accessToken: string | null;
|
|
refreshToken: string | null;
|
|
}>
|
|
) => {
|
|
const { accessToken, refreshToken } = action.payload;
|
|
if (accessToken && refreshToken) {
|
|
state.accessToken = accessToken;
|
|
saveAccessToken(accessToken);
|
|
|
|
state.refreshToken = refreshToken;
|
|
saveRefreshToken(refreshToken);
|
|
}
|
|
},
|
|
clearToken: (state) => {
|
|
state.accessToken = null;
|
|
state.refreshToken = null;
|
|
removeAccessToken();
|
|
removeRefreshToken();
|
|
},
|
|
clearDelegationToken: (state) => {
|
|
state.delegationAccessToken = null;
|
|
state.delegationRefreshToken = null;
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder.addCase(updateTokenAsync.fulfilled, (state, action) => {
|
|
const { accessToken } = action.payload;
|
|
state.accessToken = accessToken;
|
|
saveAccessToken(accessToken);
|
|
});
|
|
builder.addCase(getDelegationTokenAsync.fulfilled, (state, action) => {
|
|
const { accessToken, refreshToken } = action.payload;
|
|
state.delegationAccessToken = accessToken;
|
|
state.delegationRefreshToken = refreshToken;
|
|
});
|
|
builder.addCase(updateDelegationTokenAsync.fulfilled, (state, action) => {
|
|
const { accessToken } = action.payload;
|
|
state.delegationAccessToken = accessToken;
|
|
});
|
|
builder.addCase(updateDelegationTokenAsync.rejected, (state) => {
|
|
state.delegationAccessToken = null;
|
|
state.delegationRefreshToken = null;
|
|
});
|
|
},
|
|
});
|
|
|
|
export const { setToken, clearToken, clearDelegationToken } = authSlice.actions;
|
|
|
|
export default authSlice.reducer;
|