Skip to content

Commit 1cc5ca2

Browse files
author
Lee Richmond
committed
Make setJWT update localStorage when appropriate
So you can log in, get the jwt, and set it.
1 parent 24d8ca0 commit 1cc5ca2

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

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)