File tree Expand file tree Collapse file tree 5 files changed +37
-18
lines changed
Expand file tree Collapse file tree 5 files changed +37
-18
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { UNAUTHENTICATED } from '@/api/ErrorCodes';
55import { getSocketId } from '@/services/Pusher' ;
66
77class Api {
8- constructor ( apiUrl , authHeaderName = 'Authorization' , authHeaderPrefix = 'Bearer' ) {
8+ constructor ( apiUrl , authHeaderName = 'Authorization' ) {
99 this . axios = axios . create ( { baseURL : apiUrl } ) ;
1010
1111 this . axios
@@ -14,7 +14,7 @@ class Api {
1414 . use (
1515 config => {
1616 if ( Storage . hasToken ( ) ) {
17- config . headers [ authHeaderName ] = `${ authHeaderPrefix } ${ Storage . getToken ( ) } ` ;
17+ config . headers [ authHeaderName ] = `${ Storage . getTokenType ( ) } ${ Storage . getToken ( ) } ` ;
1818 }
1919
2020 if ( getSocketId ( ) ) {
Original file line number Diff line number Diff line change @@ -12,20 +12,18 @@ const config = {
1212
1313if ( Storage . hasToken ( ) ) {
1414 config . auth . headers = {
15- Authorization : `Bearer ${ Storage . getToken ( ) } `
15+ Authorization : `${ Storage . getTokenType ( ) } ${ Storage . getToken ( ) } `
1616 } ;
1717}
1818
1919export const pusher = new Pusher ( process . env . VUE_APP_PUSHER_APP_KEY , config ) ;
2020
21- export const getSocketId = ( ) => {
22- return pusher . connection . socket_id ;
23- } ;
21+ export const getSocketId = ( ) => pusher . connection . socket_id ;
2422
25- export const updateSocketAuthToken = ( token ) => {
26- pusher . config . auth . headers . Authorization = `Bearer ${ token } ` ;
23+ export const updateSocketAuthToken = ( tokenType , accessToken ) => {
24+ pusher . config . auth . headers . Authorization = `${ tokenType } ${ accessToken } ` ;
2725} ;
2826
2927export const removeSocketAuthToken = ( ) => {
3028 pusher . config . auth . headers = { } ;
31- }
29+ } ;
Original file line number Diff line number Diff line change 11class Storage {
22 constructor ( ) {
3- this . keyName = 'auth.token' ;
3+ this . tokenKeyName = 'auth.access_token' ;
4+ this . tokenTypeKeyName = 'auth.token_type' ;
45 this . store = window . localStorage ;
56 }
67
@@ -13,19 +14,31 @@ class Storage {
1314 }
1415
1516 getToken ( ) {
16- return this . get ( this . keyName ) ;
17+ return this . get ( this . tokenKeyName ) ;
1718 }
1819
1920 setToken ( token ) {
20- return this . set ( this . keyName , token ) ;
21+ return this . set ( this . tokenKeyName , token ) ;
2122 }
2223
2324 hasToken ( ) {
24- return ! ! this . get ( this . keyName ) ;
25+ return ! ! this . get ( this . tokenKeyName ) ;
2526 }
2627
2728 removeToken ( ) {
28- return this . store . removeItem ( this . keyName ) ;
29+ return this . store . removeItem ( this . tokenKeyName ) ;
30+ }
31+
32+ getTokenType ( ) {
33+ return this . get ( this . tokenTypeKeyName ) ;
34+ }
35+
36+ setTokenType ( type ) {
37+ return this . set ( this . tokenTypeKeyName , type ) ;
38+ }
39+
40+ removeTokenType ( ) {
41+ return this . store . removeItem ( this . tokenTypeKeyName ) ;
2942 }
3043}
3144
Original file line number Diff line number Diff line change @@ -12,7 +12,10 @@ export default {
1212 password,
1313 } ) ;
1414
15- commit ( USER_LOGIN , data . access_token ) ;
15+ commit ( USER_LOGIN , {
16+ accessToken : data . access_token ,
17+ tokenType : data . token_type
18+ } ) ;
1619 commit ( SET_LOADING , false , { root : true } ) ;
1720
1821 return Promise . resolve ( ) ;
@@ -41,7 +44,10 @@ export default {
4144 nickname,
4245 } ) ;
4346
44- commit ( USER_LOGIN , data . access_token ) ;
47+ commit ( USER_LOGIN , {
48+ accessToken : data . access_token ,
49+ tokenType : data . token_type
50+ } ) ;
4551 commit ( SET_LOADING , false , { root : true } ) ;
4652
4753 return Promise . resolve ( ) ;
Original file line number Diff line number Diff line change @@ -4,16 +4,18 @@ import { emptyUser, userMapper } from '@/services/Normalizer';
44import { updateSocketAuthToken , removeSocketAuthToken } from '@/services/Pusher' ;
55
66export default {
7- [ USER_LOGIN ] : ( state , accessToken ) => {
7+ [ USER_LOGIN ] : ( state , { accessToken, tokenType } ) => {
88 Storage . setToken ( accessToken ) ;
9- updateSocketAuthToken ( accessToken ) ;
9+ Storage . setTokenType ( tokenType ) ;
10+ updateSocketAuthToken ( tokenType , accessToken ) ;
1011
1112 state . token = accessToken ;
1213 state . isLoggedIn = true ;
1314 } ,
1415
1516 [ USER_LOGOUT ] : state => {
1617 Storage . removeToken ( ) ;
18+ Storage . removeTokenType ( ) ;
1719 removeSocketAuthToken ( ) ;
1820
1921 state . token = '' ;
You can’t perform that action at this time.
0 commit comments