1
1
import Vue from 'vue'
2
2
import Hookable from 'hookable'
3
- import isArray from 'lodash/isArray' ;
4
- import isObject from 'lodash/isObject' ;
3
+ import reqURL from 'requrl'
4
+ import { joinURL } from '@nuxt/ufo'
5
5
6
6
const TOKEN_KEY = 'strapi_jwt'
7
7
@@ -15,8 +15,13 @@ class Strapi extends Hookable {
15
15
16
16
this . $cookies = ctx . app . $cookies
17
17
this . $http = ctx . $http . create ( { } )
18
- this . $http . setToken ( this . getToken ( ) , 'Bearer' )
19
- this . $http . setBaseURL ( runtimeConfig . url || '<%= options.url %>' )
18
+ this . syncToken ( )
19
+ const url = runtimeConfig . url || '<%= options.url %>'
20
+ if ( process . server && ctx . req && url . startsWith ( '/' ) ) {
21
+ this . $http . setBaseURL ( joinURL ( reqURL ( ctx . req ) , url ) )
22
+ } else {
23
+ this . $http . setBaseURL ( url )
24
+ }
20
25
this . $http . onError ( ( err ) => {
21
26
if ( ! err . response ) {
22
27
this . callHook ( 'error' , err )
@@ -26,9 +31,9 @@ class Strapi extends Hookable {
26
31
const { response : { data : { message : msg } } } = err
27
32
28
33
let message
29
- if ( isArray ( msg ) ) {
34
+ if ( Array . isArray ( msg ) ) {
30
35
message = msg [ 0 ] . messages [ 0 ] . message
31
- } else if ( isObject ( msg ) ) {
36
+ } else if ( typeof msg === 'object' && msg !== null ) {
32
37
message = msg . message
33
38
} else {
34
39
message = msg
@@ -86,13 +91,11 @@ class Strapi extends Hookable {
86
91
}
87
92
88
93
async fetchUser ( ) {
89
- const jwt = this . getToken ( )
94
+ const jwt = this . syncToken ( )
90
95
if ( ! jwt ) {
91
96
return null
92
97
}
93
98
94
- this . $http . setToken ( jwt , 'Bearer' )
95
-
96
99
try {
97
100
const user = await this . findOne ( 'users' , 'me' )
98
101
this . setUser ( user )
@@ -145,18 +148,43 @@ class Strapi extends Hookable {
145
148
}
146
149
147
150
getToken ( ) {
148
- return this . $cookies . get ( TOKEN_KEY )
151
+ let token
152
+ if ( process . client && typeof window . localStorage !== 'undefined' ) {
153
+ token = window . localStorage . getItem ( TOKEN_KEY )
154
+ }
155
+ if ( ! token ) {
156
+ token = this . $cookies . get ( TOKEN_KEY )
157
+ }
158
+ return token
149
159
}
150
160
151
161
setToken ( jwt ) {
152
162
this . $http . setToken ( jwt , 'Bearer' )
163
+ if ( process . client && typeof window . localStorage !== 'undefined' ) {
164
+ window . localStorage . setItem ( TOKEN_KEY , jwt )
165
+ }
153
166
this . $cookies . set ( TOKEN_KEY , jwt )
154
167
}
155
168
156
169
clearToken ( ) {
157
170
this . $http . setToken ( false )
171
+ if ( process . client && typeof window . localStorage !== 'undefined' ) {
172
+ window . localStorage . removeItem ( TOKEN_KEY )
173
+ }
158
174
this . $cookies . remove ( TOKEN_KEY )
159
175
}
176
+
177
+ syncToken ( jwt ) {
178
+ if ( ! jwt ) {
179
+ jwt = this . getToken ( )
180
+ }
181
+ if ( jwt ) {
182
+ this . setToken ( jwt )
183
+ } else {
184
+ this . clearToken ( )
185
+ }
186
+ return jwt
187
+ }
160
188
}
161
189
162
190
export default async function ( ctx , inject ) {
0 commit comments