Skip to content

Commit 41ec7b7

Browse files
authored
fix(ws-header): Ensure Authorization was initialized to fetch access token changes without DOMEvent (#147)
1 parent 445dc75 commit 41ec7b7

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/ws-header/ws-header.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ import {WSDropdown} from '../ws-dropdown/ws-dropdown';
2525
*/
2626
export class WSHeader extends Component {
2727

28-
/**
29-
* @type {Authorization}
30-
*/
31-
static authorization = undefined;
32-
33-
/**
34-
* Default storage instance
35-
* @type {AbstractStorage}
36-
*/
37-
static storage = new LocalStorage();
38-
3928
static defaultProps = {
4029
loginUrl: 'https://identity.zalando.com/oauth2/authorize',
4130
businessPartnerId: null,
@@ -64,6 +53,17 @@ export class WSHeader extends Component {
6453
showAuthorization: PropTypes.bool
6554
};
6655

56+
/**
57+
* @type {Authorization}
58+
*/
59+
static authorization = new Authorization(this.storage);
60+
61+
/**
62+
* Default storage instance
63+
* @type {AbstractStorage}
64+
*/
65+
static storage = new LocalStorage('');
66+
6767
/**
6868
* Initialize the storage
6969
* @param {string} type Can be either cookie or local
@@ -77,6 +77,8 @@ export class WSHeader extends Component {
7777
} else {
7878
this.storage = new LocalStorage(name);
7979
}
80+
// Create authorization with new storage type
81+
this.authorization.storage = this.storage;
8082
}
8183

8284
/**
@@ -85,7 +87,6 @@ export class WSHeader extends Component {
8587
* @returns {string|null}
8688
*/
8789
static getAccessToken(queryString = location.hash.substr(1)) {
88-
this.authorization = this.authorization || new Authorization(this.storage);
8990
if (!this.authorization.accessToken) {
9091
this.authorization.tryFetchToken(queryString);
9192
}
@@ -97,7 +98,6 @@ export class WSHeader extends Component {
9798
* @returns {void}
9899
*/
99100
static removeAccessToken() {
100-
this.authorization = this.authorization || new Authorization(this.storage);
101101
this.authorization.unauthorize();
102102
}
103103

@@ -176,7 +176,7 @@ export class WSHeader extends Component {
176176
*/
177177
initState() {
178178
this.state = {
179-
isLoggedIn: !!(this.constructor.authorization && this.constructor.authorization.accessToken),
179+
isLoggedIn: !!this.constructor.authorization.accessToken,
180180
locale: WSHeader.getLocale()
181181
};
182182
}
@@ -186,8 +186,6 @@ export class WSHeader extends Component {
186186
* @returns {void}
187187
*/
188188
initAuthorization() {
189-
// Initialize authorization with implicit flow
190-
this.constructor.authorization = this.constructor.authorization || new Authorization(WSHeader.storage);
191189
// Listen to authorization changes
192190
this.constructor.authorization.onAccessTokenChange(accessToken => {
193191
if (this.mounted) {

0 commit comments

Comments
 (0)