Skip to content

Commit 5e741b6

Browse files
authored
Merge pull request #29 from richmolj/master
Make setJWT update localStorage when appropriate
2 parents 4419026 + 1cc5ca2 commit 5e741b6

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsorm",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"description": "Javascript ORM",
55
"main": "_bundles/jsorm.js",
66
"module": "lib-esm/index.js",

src/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export default class Model {
6363

6464
static setJWT(token: string) : void {
6565
this.getJWTOwner().jwt = token;
66+
67+
if (Config.jwtLocalStorage) {
68+
Config.localStorage.setItem(Config.jwtLocalStorage, token)
69+
}
6670
}
6771

6872
static getJWT() : string {

src/util/refresh-jwt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import Config from '../configuration';
33

44
export default function refreshJWT(klass: typeof Model, serverResponse: Response) : void {
55
let jwt = serverResponse.headers.get('X-JWT');
6-
let localStorage = Config.localStorage;
6+
if (!jwt) return
77

8+
let localStorage = Config.localStorage;
89
if (localStorage) {
910
let localStorageKey = Config.jwtLocalStorage;
1011
if (localStorageKey) {

test/unit/model-test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ describe('Model', function() {
132132
Author.setJWT('n3wt0k3n');
133133
expect(ApplicationRecord.jwt).to.eq('n3wt0k3n');
134134
});
135+
136+
describe.only('when localStorage is configured', function() {
137+
beforeEach(function() {
138+
Config.jwtLocalStorage = 'jwt'
139+
Config.localStorage = { setItem: sinon.spy() }
140+
})
141+
142+
afterEach(function() {
143+
Config.jwtLocalStorage = undefined
144+
Config.localStorage = undefined
145+
})
146+
147+
it('adds to localStorage', function() {
148+
Author.setJWT('n3wt0k3n');
149+
let called = Config.localStorage.setItem
150+
.calledWith('jwt', 'n3wt0k3n');
151+
expect(called).to.eq(true);
152+
})
153+
})
135154
});
136155

137156
describe('#fetchOptions', function() {

0 commit comments

Comments
 (0)