@@ -4,16 +4,15 @@ import HTTP_STATUS from './httpStatus';
4
4
5
5
const api = axios . create ( ) ;
6
6
7
- // api.interceptors.request.use(async config => {
8
- // const token = localStorage.getItem('access_token');
7
+ api . interceptors . request . use ( async config => {
8
+ const token = localStorage . getItem ( 'access_token' ) ;
9
9
10
- // if (token) {
11
- // config.headers.Authorization = `Bearer ${token}`;
12
- // }
13
- // console.log(config);
10
+ if ( token ) {
11
+ config . headers . Authorization = `Bearer ${ token } ` ;
12
+ }
14
13
15
- // return config;
16
- // });
14
+ return config ;
15
+ } ) ;
17
16
18
17
api . interceptors . response . use (
19
18
response => {
@@ -24,14 +23,16 @@ api.interceptors.response.use(
24
23
// トークンの有効期限が切れていて、トークン更新要求がない場合
25
24
if ( error . response . status === 401 && ! originalRequest . _retry ) {
26
25
originalRequest . _retry = true ;
27
- const refreshToken = localStorage . getItem ( 'refresh_token' ) ;
26
+ const body = {
27
+ refresh_token : localStorage . getItem ( 'refresh_token' ) ,
28
+ } ;
28
29
try {
29
- const res = await axios . post ( `${ BASE_URLS . gin } /auth/refresh-token` , {
30
- refreshToken,
31
- } ) ;
30
+ const res = await axios . post (
31
+ `${ BASE_URLS . gin } /auth/google/refresh-token` ,
32
+ body
33
+ ) ;
32
34
if ( res . status === 200 ) {
33
- const newToken = res . data . access_token ;
34
- console . log ( newToken ) ;
35
+ const newToken = res . data . data . access_token ;
35
36
localStorage . setItem ( 'access_token' , newToken ) ;
36
37
api . defaults . headers . common [ 'Authorization' ] = `Bearer ${ newToken } ` ;
37
38
// トークンが更新された後、元のリクエストを再実行
@@ -52,10 +53,6 @@ const req = async (
52
53
body : object | undefined = undefined
53
54
) => {
54
55
api . defaults . baseURL = BASE_URLS [ server ] ;
55
- if ( server === 'nest' ) {
56
- api . defaults . headers . common [ 'Authorization' ] =
57
- process . env . NEXT_PUBLIC_NEST_TOKEN ;
58
- }
59
56
60
57
if ( typeof body !== 'undefined' ) {
61
58
if ( body instanceof FormData ) {
0 commit comments