@@ -8,7 +8,7 @@ import { WritePayload } from "./util/write-payload"
8
8
import { flipEnumerable , getNonEnumerables } from "./util/enumerables"
9
9
import {
10
10
CredentialStorage ,
11
- NullStorageBackend ,
11
+ InMemoryStorageBackend ,
12
12
StorageBackend
13
13
} from "./credential-storage"
14
14
import { IDMap } from "./id-map"
@@ -118,11 +118,18 @@ export const applyModelConfig = <T extends typeof JSORMBase>(
118
118
let k : keyof ModelConfigurationOptions
119
119
120
120
for ( k in config ) {
121
- if ( config . hasOwnProperty ( k ) ) {
121
+ if ( config . hasOwnProperty ( k ) && k !== 'jwt' ) {
122
122
ModelClass [ k ] = config [ k ]
123
123
}
124
124
}
125
125
126
+ // Run this one last, since either the Credential Storage strategy
127
+ // or the store key could be set in options alongside this, otherwise
128
+ // this is dependent on option declaration order
129
+ if ( config . jwt ) {
130
+ ModelClass . setJWT ( config . jwt )
131
+ }
132
+
126
133
if ( ModelClass . isBaseClass === undefined ) {
127
134
ModelClass . setAsBase ( )
128
135
} else if ( ModelClass . isBaseClass === true ) {
@@ -136,7 +143,6 @@ export class JSORMBase {
136
143
static jsonapiType ?: string
137
144
static endpoint : string
138
145
static isBaseClass : boolean
139
- static jwt ?: string
140
146
static keyCase : KeyCase = { server : "snake" , client : "camel" }
141
147
static strictAttributes : boolean = false
142
148
static logger : ILogger = defaultLogger
@@ -148,36 +154,48 @@ export class JSORMBase {
148
154
static currentClass : typeof JSORMBase = JSORMBase
149
155
static beforeFetch : BeforeFilter | undefined
150
156
static afterFetch : AfterFilter | undefined
151
- static jwtStorage : string | false = "jwt"
152
157
153
158
private static _typeRegistry : JsonapiTypeRegistry
154
159
private static _IDMap : IDMap
155
160
private static _middlewareStack : MiddlewareStack
156
- private static _credentialStorageBackend ?: StorageBackend
157
- private static _credentialStorage ?: CredentialStorage
161
+ private static _credentialStorageBackend : StorageBackend
162
+ private static _credentialStorage : CredentialStorage
163
+ private static _jwtStorage : string | false = "jwt"
158
164
159
165
static get credentialStorage ( ) : CredentialStorage {
160
- if ( ! this . _credentialStorage ) {
161
- if ( ! this . _credentialStorageBackend ) {
162
- if ( this . jwtStorage && typeof localStorage !== "undefined" ) {
163
- this . _credentialStorageBackend = localStorage
164
- } else {
165
- this . _credentialStorageBackend = new NullStorageBackend ( )
166
- }
167
- }
166
+ return this . _credentialStorage
167
+ }
168
168
169
- this . _credentialStorage = new CredentialStorage (
170
- this . jwtStorage ,
171
- this . _credentialStorageBackend
172
- )
169
+ static set jwtStorage ( val : string | false ) {
170
+ if ( val !== this . _jwtStorage ) {
171
+ this . _jwtStorage = val
172
+ this . credentialStorageBackend = this . _credentialStorageBackend
173
173
}
174
+ }
174
175
175
- return this . _credentialStorage
176
+ static get jwtStorage ( ) : string | false {
177
+ return this . _jwtStorage
176
178
}
177
179
178
- static set credentialStorageBackend ( backend : StorageBackend | undefined ) {
180
+ static set credentialStorageBackend ( backend : StorageBackend ) {
179
181
this . _credentialStorageBackend = backend
180
- this . _credentialStorage = undefined
182
+
183
+ this . _credentialStorage = new CredentialStorage (
184
+ this . jwtStorage || 'jwt' ,
185
+ this . _credentialStorageBackend
186
+ )
187
+ }
188
+
189
+ static get credentialStorageBackend ( ) : StorageBackend {
190
+ return this . _credentialStorageBackend
191
+ }
192
+
193
+ static initializeCredentialStorage ( ) {
194
+ if ( this . jwtStorage && typeof localStorage !== "undefined" ) {
195
+ this . credentialStorageBackend = localStorage
196
+ } else {
197
+ this . credentialStorageBackend = new InMemoryStorageBackend ( )
198
+ }
181
199
}
182
200
183
201
/*
@@ -228,9 +246,6 @@ export class JSORMBase {
228
246
if ( ! this . _IDMap ) {
229
247
this . _IDMap = new IDMap ( )
230
248
}
231
-
232
- const jwt = this . credentialStorage . getJWT ( )
233
- this . setJWT ( jwt )
234
249
}
235
250
236
251
static isSubclassOf ( maybeSuper : typeof JSORMBase ) : boolean {
@@ -753,22 +768,16 @@ export class JSORMBase {
753
768
}
754
769
755
770
static setJWT ( token : string | undefined | null ) : void {
756
- if ( this . baseClass === undefined ) {
757
- throw new Error ( `Cannot set JWT on ${ this . name } : No base class present.` )
758
- }
759
-
760
- this . baseClass . jwt = token || undefined
761
771
this . credentialStorage . setJWT ( token )
762
772
}
763
773
764
774
static getJWT ( ) : string | undefined {
765
- const owner = this . baseClass
766
-
767
- if ( owner ) {
768
- return owner . jwt
769
- }
775
+ return this . credentialStorage . getJWT ( )
770
776
}
771
777
778
+ static get jwt ( ) : string | undefined { return this . getJWT ( ) }
779
+ static set jwt ( token : string | undefined ) { this . setJWT ( token ) }
780
+
772
781
static generateAuthHeader ( jwt : string ) : string {
773
782
return `Token token="${ jwt } "`
774
783
}
@@ -897,6 +906,7 @@ export class JSORMBase {
897
906
}
898
907
899
908
; ( < any > JSORMBase . prototype ) . klass = JSORMBase
909
+ JSORMBase . initializeCredentialStorage ( )
900
910
901
911
export const isModelClass = ( arg : any ) : arg is typeof JSORMBase => {
902
912
if ( ! arg ) {
0 commit comments