Skip to content

Commit

Permalink
fix: auth bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
HyeokE committed Mar 13, 2024
1 parent c0b2a6c commit 3071cb4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
26 changes: 22 additions & 4 deletions src/apis/Instance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import axios from 'axios';

import { nativeInfo } from '@utils/storage';
import BridgeApi from '@utils/Bridge.ts';
import { nativeInfo } from '@utils/storage.ts';

export const Instance = axios.create({
baseURL: 'https://api.jmt-matzip.dev',
headers: {
authorization: `Bearer ${nativeInfo.getData().accessToken}`,
},
});

Instance.interceptors.request.use((config) => {
const token = nativeInfo.getData().accessToken;
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});

Instance.interceptors.response.use(
(res) => res,
async (error) => {
/** 1 */
if (error.response?.status === 401) {
BridgeApi.token();
}

return Promise.reject(error);
},
);
10 changes: 8 additions & 2 deletions src/pages/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode } from 'react';
import { ReactNode, useRef } from 'react';

import RightArrowIconButton from '@components/common/RightArrowIcon.tsx';
import CreateGroupButton from '@components/Main/CreateGroupButton.tsx';
Expand All @@ -7,14 +7,17 @@ import { AppScreen } from '@stackflow/plugin-basic-ui';
import { useMainFlow } from '@stacks/StackFlow';
import { MainContainer, MyGroupList, MyGroupListContainer, MyGroupListHeader } from '@styles/pages/Main.tsx';
import { textStyles } from '@styles/theme/typographies.ts';
import BridgeApi from '@utils/Bridge.ts';
import BridgeApi, { setAccessToken } from '@utils/Bridge.ts';
import { nativeInfo } from '@utils/storage.ts';

import { useGetMyGroups } from '../apis/Group/queries/useGetMyGroups.ts';

const Main = (): ReactNode => {
const { push } = useMainFlow();
const { myGroupsData } = useGetMyGroups();

const inputRef = useRef<HTMLInputElement>(null);

return (
<AppScreen
appBar={{
Expand All @@ -41,6 +44,9 @@ const Main = (): ReactNode => {
<button onClick={() => BridgeApi.navigation(true)}>nav 노출</button>
<button onClick={() => BridgeApi.navigation(false)}>nav 비노출</button>
<button onClick={() => BridgeApi.token()}>토큰 가져오기</button>
<input ref={inputRef} />
<button onClick={() => setAccessToken(inputRef.current?.value ?? '')}>토큰 세팅</button>
<button onClick={() => console.info(nativeInfo.getData())}>토큰 체크</button>
</MainContainer>
</AppScreen>
);
Expand Down
3 changes: 1 addition & 2 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type NativeInfo = {
};

export const nativeInfo = makeStorage<NativeInfo>({
accessToken:
'eyJhbGciOiJIUzUxMiJ9.eyJlbWFpbCI6InRlc3RAbmF2ZXIuY29tIiwiYXV0aCI6Ik1FTUJFUiIsImV4cCI6MTcxMDA4MjUxMX0.TChmWo1TeOtJZzeuHikhDJLdfKviTHkNVTFux5Up9_peKyFZKqIspuc3haVLhcSetOMdqIK3sCVi4iDyNFFu9Q',
accessToken: '',
userPosition: {
placeName: '',
addressName: '',
Expand Down

0 comments on commit 3071cb4

Please sign in to comment.