1
+ import Cookies from 'js-cookie' ;
1
2
import BASE_URLS from './baseUrl' ;
2
3
import HTTP_STATUS from './httpStatus' ;
3
4
4
5
async function fetchWithInterceptors ( url : string , options : RequestInit ) {
5
6
let response = await fetch ( url , options ) ;
6
7
7
8
if ( response . status === HTTP_STATUS . UNAUTHORIZED ) {
8
- const refreshToken = localStorage . getItem ( 'refresh_token' ) ;
9
+ const refreshToken = Cookies . get ( 'refresh_token' ) ;
9
10
const refreshResponse = await fetch (
10
11
`${ BASE_URLS . gin } /auth/google/refresh-token` ,
11
12
{
@@ -20,7 +21,7 @@ async function fetchWithInterceptors(url: string, options: RequestInit) {
20
21
if ( refreshResponse . ok ) {
21
22
const data = await refreshResponse . json ( ) ;
22
23
const newToken = data . data . access_token ;
23
- localStorage . setItem ( 'access_token' , newToken ) ;
24
+ Cookies . set ( 'access_token' , newToken ) ;
24
25
options . headers = {
25
26
...options . headers ,
26
27
Authorization : `Bearer ${ newToken } ` ,
@@ -47,7 +48,7 @@ const req = async (
47
48
body : BodyInit | object | undefined = undefined
48
49
) => {
49
50
const headers = new Headers ( ) ;
50
- const token = localStorage . getItem ( 'access_token' ) ;
51
+ const token = Cookies . get ( 'access_token' ) ;
51
52
if ( token ) {
52
53
headers . append ( 'Authorization' , `Bearer ${ token } ` ) ;
53
54
}
0 commit comments