@@ -8,7 +8,7 @@ import { WritePayload } from "./util/write-payload"
88import { flipEnumerable , getNonEnumerables } from "./util/enumerables"
99import {
1010 CredentialStorage ,
11- NullStorageBackend ,
11+ InMemoryStorageBackend ,
1212 StorageBackend
1313} from "./credential-storage"
1414import { IDMap } from "./id-map"
@@ -118,11 +118,18 @@ export const applyModelConfig = <T extends typeof JSORMBase>(
118118 let k : keyof ModelConfigurationOptions
119119
120120 for ( k in config ) {
121- if ( config . hasOwnProperty ( k ) ) {
121+ if ( config . hasOwnProperty ( k ) && k !== 'jwt' ) {
122122 ModelClass [ k ] = config [ k ]
123123 }
124124 }
125125
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+
126133 if ( ModelClass . isBaseClass === undefined ) {
127134 ModelClass . setAsBase ( )
128135 } else if ( ModelClass . isBaseClass === true ) {
@@ -136,7 +143,6 @@ export class JSORMBase {
136143 static jsonapiType ?: string
137144 static endpoint : string
138145 static isBaseClass : boolean
139- static jwt ?: string
140146 static keyCase : KeyCase = { server : "snake" , client : "camel" }
141147 static strictAttributes : boolean = false
142148 static logger : ILogger = defaultLogger
@@ -148,36 +154,48 @@ export class JSORMBase {
148154 static currentClass : typeof JSORMBase = JSORMBase
149155 static beforeFetch : BeforeFilter | undefined
150156 static afterFetch : AfterFilter | undefined
151- static jwtStorage : string | false = "jwt"
152157
153158 private static _typeRegistry : JsonapiTypeRegistry
154159 private static _IDMap : IDMap
155160 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"
158164
159165 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+ }
168168
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
173173 }
174+ }
174175
175- return this . _credentialStorage
176+ static get jwtStorage ( ) : string | false {
177+ return this . _jwtStorage
176178 }
177179
178- static set credentialStorageBackend ( backend : StorageBackend | undefined ) {
180+ static set credentialStorageBackend ( backend : StorageBackend ) {
179181 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+ }
181199 }
182200
183201 /*
@@ -228,9 +246,6 @@ export class JSORMBase {
228246 if ( ! this . _IDMap ) {
229247 this . _IDMap = new IDMap ( )
230248 }
231-
232- const jwt = this . credentialStorage . getJWT ( )
233- this . setJWT ( jwt )
234249 }
235250
236251 static isSubclassOf ( maybeSuper : typeof JSORMBase ) : boolean {
@@ -753,22 +768,16 @@ export class JSORMBase {
753768 }
754769
755770 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
761771 this . credentialStorage . setJWT ( token )
762772 }
763773
764774 static getJWT ( ) : string | undefined {
765- const owner = this . baseClass
766-
767- if ( owner ) {
768- return owner . jwt
769- }
775+ return this . credentialStorage . getJWT ( )
770776 }
771777
778+ static get jwt ( ) : string | undefined { return this . getJWT ( ) }
779+ static set jwt ( token : string | undefined ) { this . setJWT ( token ) }
780+
772781 static generateAuthHeader ( jwt : string ) : string {
773782 return `Token token="${ jwt } "`
774783 }
@@ -897,6 +906,7 @@ export class JSORMBase {
897906}
898907
899908; ( < any > JSORMBase . prototype ) . klass = JSORMBase
909+ JSORMBase . initializeCredentialStorage ( )
900910
901911export const isModelClass = ( arg : any ) : arg is typeof JSORMBase => {
902912 if ( ! arg ) {
0 commit comments