Skip to content

Commit

Permalink
feat: 웹뷰일 때 토큰 갱신 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
alstn2468 committed Dec 14, 2024
1 parent 82e70ca commit 6f2e5f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
},
"dependencies": {
"@boolti/bridge": "*",
"@emotion/react": "^11.11.3",
"@lukemorales/query-key-factory": "^1.3.2",
"@tanstack/query-core": "^4.32.6",
Expand Down
23 changes: 18 additions & 5 deletions packages/api/src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ky, { HTTPError } from 'ky';

import { isBooltiHTTPError } from './BooltiHTTPError';
import { LOCAL_STORAGE } from './constants';
import { checkIsWebView, isWebViewBridgeAvailable, requestToken } from '@boolti/bridge';

const API_URL = import.meta.env.VITE_BASE_API_URL;
const IS_SUPER_ADMIN = import.meta.env.VITE_IS_SUPER_ADMIN === 'true';
Expand Down Expand Up @@ -52,13 +53,25 @@ export const instance = ky.create({
async (request, options, response) => {
// access token이 만료되었을 때, refresh token으로 새로운 access token을 발급받는다.
if (!response.ok && response.status === 401 && !request.url.includes('logout')) {
let newAccessToken: string | undefined = undefined,
newRefreshToken: string | undefined = undefined;
try {
const { accessToken, refreshToken } = (await postRefreshToken()) ?? {};
if (accessToken && refreshToken) {
window.localStorage.setItem(LOCAL_STORAGE.ACCESS_TOKEN, accessToken);
window.localStorage.setItem(LOCAL_STORAGE.REFRESH_TOKEN, refreshToken);
if (checkIsWebView() && isWebViewBridgeAvailable()) {
newAccessToken = (await requestToken()).data.token;
} else {
const { accessToken, refreshToken } = (await postRefreshToken()) ?? {};
newAccessToken = accessToken;
newRefreshToken = refreshToken;
}

if (newAccessToken) {
window.localStorage.setItem(LOCAL_STORAGE.ACCESS_TOKEN, newAccessToken);

if (newRefreshToken) {
window.localStorage.setItem(LOCAL_STORAGE.REFRESH_TOKEN, newRefreshToken);
}

request.headers.set('Authorization', `Bearer ${accessToken}`);
request.headers.set('Authorization', `Bearer ${newAccessToken}`);

return ky(request, options);
}
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@boolti/api@workspace:packages/api"
dependencies:
"@boolti/bridge": "npm:*"
"@boolti/eslint-config": "npm:*"
"@boolti/typescript-config": "npm:*"
"@emotion/react": "npm:^11.11.3"
Expand Down

0 comments on commit 6f2e5f9

Please sign in to comment.