Skip to content

Commit f4f7558

Browse files
committed
add instance code to key
1 parent e036534 commit f4f7558

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

projects/fusio-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-fusio-sdk",
3-
"version": "5.1.1",
3+
"version": "5.1.2",
44
"description": "SDK to integrate Fusio into an Angular app",
55
"keywords": [
66
"Fusio",

projects/fusio-sdk/src/lib/service/api.service.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import {ConfigService} from "./config.service";
77
})
88
export abstract class ApiService<T extends ClientAbstract> {
99

10-
private readonly baseUrl: string;
11-
private readonly store: TokenStoreInterface;
10+
private baseUrl: string;
11+
private store: TokenStoreInterface;
1212

1313
constructor(private config: ConfigService) {
14-
let baseUrl = this.config.getBaseUrl();
15-
16-
this.baseUrl = ApiService.normalizeBaseUrl(baseUrl);
14+
this.baseUrl = '';
1715
this.store = new SessionTokenStore();
16+
this.setBaseUrl(this.config.getBaseUrl());
1817
}
1918

2019
public getClientWithCredentials(clientId: string, clientSecret: string): T {
@@ -54,6 +53,15 @@ export abstract class ApiService<T extends ClientAbstract> {
5453
return this.baseUrl;
5554
}
5655

56+
public setBaseUrl(baseUrl: string) {
57+
this.baseUrl = ApiService.normalizeBaseUrl(baseUrl);
58+
this.store = new SessionTokenStore('fusio_access_token_' + this.config.getInstanceCode());
59+
}
60+
61+
public getTokenStore(): TokenStoreInterface {
62+
return this.store;
63+
}
64+
5765
public hasValidToken(): boolean {
5866
const token = this.store.get();
5967
if (!token) {
@@ -87,8 +95,7 @@ export abstract class ApiService<T extends ClientAbstract> {
8795
}
8896
}
8997

90-
private getExpiresInTimestamp(expiresIn: number): number
91-
{
98+
private getExpiresInTimestamp(expiresIn: number): number {
9299
const nowTimestamp = Math.floor(Date.now() / 1000);
93100

94101
if (expiresIn < 529196400) {

projects/fusio-sdk/src/lib/service/config.service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,20 @@ export class ConfigService {
146146
}
147147
}
148148

149+
/**
150+
* @see https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
151+
*/
152+
public getInstanceCode(): number {
153+
let hash = 0;
154+
if (this.config.baseUrl.length === 0) {
155+
return hash;
156+
}
157+
158+
for (let i = 0; i < this.config.baseUrl.length; i++) {
159+
hash = ((hash << 5) - hash) + this.config.baseUrl.charCodeAt(i);
160+
hash |= 0;
161+
}
162+
163+
return hash;
164+
}
149165
}

projects/fusio-sdk/src/lib/service/user.service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Injectable} from '@angular/core';
22
import {ConsumerUserAccount} from "fusio-sdk";
33
import {FusioService} from "./fusio.service";
44
import {EventService} from "./event.service";
5+
import {ConfigService} from "./config.service";
56

67
@Injectable({
78
providedIn: 'root'
@@ -10,11 +11,11 @@ export class UserService {
1011

1112
private user?: ConsumerUserAccount;
1213

13-
constructor(private fusio: FusioService, private event: EventService) { }
14+
constructor(private fusio: FusioService, private config: ConfigService, private event: EventService) { }
1415

1516
public login(user: ConsumerUserAccount): void {
1617
this.user = user;
17-
sessionStorage.setItem('fusio_user', JSON.stringify(user));
18+
sessionStorage.setItem(this.getKey(), JSON.stringify(user));
1819

1920
this.event.dispatchLogin(user);
2021
}
@@ -28,7 +29,7 @@ export class UserService {
2829
return this.user;
2930
}
3031

31-
const rawData = sessionStorage.getItem('fusio_user');
32+
const rawData = sessionStorage.getItem(this.getKey());
3233
if (!rawData) {
3334
return undefined;
3435
}
@@ -38,10 +39,13 @@ export class UserService {
3839

3940
public logout(): void {
4041
this.user = undefined;
41-
sessionStorage.removeItem('fusio_user');
42+
sessionStorage.removeItem(this.getKey());
4243
this.fusio.logout();
4344

4445
this.event.dispatchLogout();
4546
}
4647

48+
private getKey(): string {
49+
return 'fusio_user_' + this.config.getInstanceCode();
50+
}
4751
}

0 commit comments

Comments
 (0)