From 6f2e5f9d6e8a301177c4cbbc592c6f3daf6ad547 Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Sun, 15 Dec 2024 00:56:44 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9B=B9=EB=B7=B0=EC=9D=BC=20=EB=95=8C?= =?UTF-8?q?=20=ED=86=A0=ED=81=B0=20=EA=B0=B1=EC=8B=A0=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pnp.cjs | 1 + packages/api/package.json | 1 + packages/api/src/fetcher.ts | 23 ++++++++++++++++++----- yarn.lock | 1 + 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.pnp.cjs b/.pnp.cjs index fe199c32..f9e62327 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -2941,6 +2941,7 @@ const RAW_RUNTIME_STATE = "packageLocation": "./packages/api/",\ "packageDependencies": [\ ["@boolti/api", "workspace:packages/api"],\ + ["@boolti/bridge", "workspace:packages/bridge"],\ ["@boolti/eslint-config", "workspace:packages/config-eslint"],\ ["@boolti/typescript-config", "workspace:packages/config-typescript"],\ ["@emotion/react", "virtual:de80dc576383b2386358abc0e9fe49c00e3397fe355a0337462b73ab3115c2e557eb85784ee0fe776394cc11dd020b4e84dbbd75acf72ee6d54415d82d21f5c5#npm:11.11.3"],\ diff --git a/packages/api/package.json b/packages/api/package.json index 6d95959a..2ba01a48 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -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", diff --git a/packages/api/src/fetcher.ts b/packages/api/src/fetcher.ts index 9793e6d3..c062a614 100644 --- a/packages/api/src/fetcher.ts +++ b/packages/api/src/fetcher.ts @@ -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'; @@ -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); } diff --git a/yarn.lock b/yarn.lock index a85704b1..483b7538 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"