Skip to content

Commit a81a51f

Browse files
authored
feat: Oauth type 추가 (#29)
* refactor: 코드 순서 수정 * feat: oauth type 분리
1 parent d14dcaa commit a81a51f

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

src/lib/types/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//////////////////////////
2+
// product domain //
3+
//////////////////////////
4+
15
// product
26
export * from './product.type';
37

@@ -16,8 +20,15 @@ export * from './review.type';
1620
// session
1721
export * from './session.type';
1822

23+
///////////////////////
24+
// common type //
25+
///////////////////////
26+
1927
// storage
2028
export * from './storage.type';
2129

2230
// weekday
2331
export * from './weekday.type';
32+
33+
// oauth provider
34+
export * from './ouath-provider.type';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type OAuthProviderType = 'NAVER' | 'KAKAO' | 'APPLE' | 'BASIC';

src/lib/types/session.type.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {OAuthProviderType} from './ouath-provider.type';
2+
13
export type SessionType = {
24
accessToken: string;
35
refreshToken?: string;
@@ -9,5 +11,5 @@ export type SessionType = {
911
* @description refreshToken 만료 시간 (Oauth 로그인 시)
1012
*/
1113
refreshTokenExpiresAt?: number;
12-
OAuthProvider: 'NAVER' | 'KAKAO' | 'APPLE' | 'BASIC';
14+
OAuthProvider: OAuthProviderType;
1315
};

src/network/api-client/api-client.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,9 @@ class ApiClient {
2020

2121
private sessionOptions: ApiClientOptions['sessionOptions'];
2222

23-
private async expiredSession() {
24-
await this.sessionOptions.setStorage('session', {});
25-
this._jwt = null;
26-
const expiredError = new CustomError({
27-
errorCode: 401,
28-
errorMessage: '세션이 만료되어 로그아웃되었습니다.',
29-
});
30-
throw expiredError;
31-
}
32-
33-
private async setAuthorizationHeader(config: InternalAxiosRequestConfig): Promise<void> {
34-
const session: SessionType | null = await this.sessionOptions.getStorage('session');
35-
this._jwt = session?.accessToken ?? null;
36-
37-
if (!this._jwt) {
38-
return;
39-
}
40-
41-
// accessToken이 있으면 Authorization 헤더에 추가
42-
if (this._jwt) {
43-
config.headers.Authorization = `Bearer ${this._jwt}`;
44-
} else {
45-
config.headers.Authorization = null;
46-
}
47-
}
48-
4923
private constructor({serverApiBaseUrl, sessionOptions}: ApiClientOptions) {
5024
this.sessionOptions = sessionOptions;
25+
5126
this.axiosInstance = axios.create({
5227
baseURL: serverApiBaseUrl,
5328
headers: {
@@ -107,6 +82,32 @@ class ApiClient {
10782
);
10883
}
10984

85+
private async expiredSession() {
86+
await this.sessionOptions.setStorage('session', {});
87+
this._jwt = null;
88+
const expiredError = new CustomError({
89+
errorCode: 401,
90+
errorMessage: '세션이 만료되어 로그아웃되었습니다.',
91+
});
92+
throw expiredError;
93+
}
94+
95+
private async setAuthorizationHeader(config: InternalAxiosRequestConfig): Promise<void> {
96+
const session: SessionType | null = await this.sessionOptions.getStorage('session');
97+
this._jwt = session?.accessToken ?? null;
98+
99+
if (!this._jwt) {
100+
return;
101+
}
102+
103+
// accessToken이 있으면 Authorization 헤더에 추가
104+
if (this._jwt) {
105+
config.headers.Authorization = `Bearer ${this._jwt}`;
106+
} else {
107+
config.headers.Authorization = null;
108+
}
109+
}
110+
110111
public static getInstance(): ApiClient {
111112
if (!ApiClient.instance) {
112113
throw new Error('ApiClient is not initialized:\nApiClient.create() must be called before getInstance()');

0 commit comments

Comments
 (0)