@@ -4,8 +4,6 @@ import {LocalStorage} from './storage/local-storage';
4
4
import { Authorization } from './authorization' ;
5
5
import { WSDropdown } from '../ws-dropdown/ws-dropdown' ;
6
6
7
- let authorization ;
8
-
9
7
/**
10
8
* This component renders a generic header which provides authentication and language management
11
9
*
@@ -15,6 +13,11 @@ let authorization;
15
13
*/
16
14
export class WSHeader extends Component {
17
15
16
+ /**
17
+ * @type {Authorization }
18
+ */
19
+ static authorization = undefined ;
20
+
18
21
/**
19
22
* Default storage instance
20
23
* @type {AbstractStorage }
@@ -64,11 +67,20 @@ export class WSHeader extends Component {
64
67
* @returns {string|null }
65
68
*/
66
69
static getAccessToken ( queryString = location . hash . substr ( 1 ) ) {
67
- authorization = authorization || new Authorization ( this . storage ) ;
68
- if ( ! authorization . accessToken ) {
69
- authorization . tryFetchToken ( queryString ) ;
70
+ this . authorization = this . authorization || new Authorization ( this . storage ) ;
71
+ if ( ! this . authorization . accessToken ) {
72
+ this . authorization . tryFetchToken ( queryString ) ;
70
73
}
71
- return authorization . accessToken ;
74
+ return this . authorization . accessToken ;
75
+ }
76
+
77
+ /**
78
+ * Unauthorize will remove the access token from storage
79
+ * @returns {void }
80
+ */
81
+ static removeAccessToken ( ) {
82
+ this . authorization = this . authorization || new Authorization ( this . storage ) ;
83
+ this . authorization . unauthorize ( ) ;
72
84
}
73
85
74
86
/**
@@ -141,9 +153,9 @@ export class WSHeader extends Component {
141
153
*/
142
154
initAuthorization ( props ) {
143
155
// Initialize authorization with implicit flow
144
- authorization = authorization || new Authorization ( WSHeader . storage ) ;
156
+ this . constructor . authorization = this . constructor . authorization || new Authorization ( WSHeader . storage ) ;
145
157
// Listen to authorization changes
146
- authorization . onAccessTokenChange ( accessToken => {
158
+ this . constructor . authorization . onAccessTokenChange ( accessToken => {
147
159
if ( this . mounted ) {
148
160
this . setState ( { isLoggedIn : ! ! accessToken } ) ;
149
161
} else {
@@ -153,14 +165,14 @@ export class WSHeader extends Component {
153
165
this . dispatchEvent ( 'ws-auth-changed' , accessToken ) ;
154
166
} ) ;
155
167
// Check if we was redirected from the auth page and an access token is available
156
- authorization . tryFetchToken ( location . hash . substr ( 1 ) ) ;
168
+ this . constructor . authorization . tryFetchToken ( location . hash . substr ( 1 ) ) ;
157
169
// Listen for authentication requests
158
170
window . addEventListener ( 'ws-authorize' , ( ) => {
159
- authorization . authorize ( props . loginUrl , props . clientId , props . businessPartnerId ) ;
171
+ this . constructor . authorization . authorize ( props . loginUrl , props . clientId , props . businessPartnerId ) ;
160
172
} ) ;
161
173
// Listen for authentication removal requests
162
174
window . addEventListener ( 'ws-unauthorize' , ( ) => {
163
- authorization . unauthorize ( ) ;
175
+ this . constructor . authorization . unauthorize ( ) ;
164
176
} ) ;
165
177
}
166
178
0 commit comments