Skip to content

Commit

Permalink
add instance code to key
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Dec 16, 2024
1 parent e036534 commit f4f7558
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion projects/fusio-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-fusio-sdk",
"version": "5.1.1",
"version": "5.1.2",
"description": "SDK to integrate Fusio into an Angular app",
"keywords": [
"Fusio",
Expand Down
21 changes: 14 additions & 7 deletions projects/fusio-sdk/src/lib/service/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {ConfigService} from "./config.service";
})
export abstract class ApiService<T extends ClientAbstract> {

private readonly baseUrl: string;
private readonly store: TokenStoreInterface;
private baseUrl: string;
private store: TokenStoreInterface;

constructor(private config: ConfigService) {
let baseUrl = this.config.getBaseUrl();

this.baseUrl = ApiService.normalizeBaseUrl(baseUrl);
this.baseUrl = '';
this.store = new SessionTokenStore();
this.setBaseUrl(this.config.getBaseUrl());
}

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

public setBaseUrl(baseUrl: string) {
this.baseUrl = ApiService.normalizeBaseUrl(baseUrl);
this.store = new SessionTokenStore('fusio_access_token_' + this.config.getInstanceCode());
}

public getTokenStore(): TokenStoreInterface {
return this.store;
}

public hasValidToken(): boolean {
const token = this.store.get();
if (!token) {
Expand Down Expand Up @@ -87,8 +95,7 @@ export abstract class ApiService<T extends ClientAbstract> {
}
}

private getExpiresInTimestamp(expiresIn: number): number
{
private getExpiresInTimestamp(expiresIn: number): number {
const nowTimestamp = Math.floor(Date.now() / 1000);

if (expiresIn < 529196400) {
Expand Down
16 changes: 16 additions & 0 deletions projects/fusio-sdk/src/lib/service/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,20 @@ export class ConfigService {
}
}

/**
* @see https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
*/
public getInstanceCode(): number {
let hash = 0;
if (this.config.baseUrl.length === 0) {
return hash;
}

for (let i = 0; i < this.config.baseUrl.length; i++) {
hash = ((hash << 5) - hash) + this.config.baseUrl.charCodeAt(i);
hash |= 0;
}

return hash;
}
}
12 changes: 8 additions & 4 deletions projects/fusio-sdk/src/lib/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Injectable} from '@angular/core';
import {ConsumerUserAccount} from "fusio-sdk";
import {FusioService} from "./fusio.service";
import {EventService} from "./event.service";
import {ConfigService} from "./config.service";

@Injectable({
providedIn: 'root'
Expand All @@ -10,11 +11,11 @@ export class UserService {

private user?: ConsumerUserAccount;

constructor(private fusio: FusioService, private event: EventService) { }
constructor(private fusio: FusioService, private config: ConfigService, private event: EventService) { }

public login(user: ConsumerUserAccount): void {
this.user = user;
sessionStorage.setItem('fusio_user', JSON.stringify(user));
sessionStorage.setItem(this.getKey(), JSON.stringify(user));

this.event.dispatchLogin(user);
}
Expand All @@ -28,7 +29,7 @@ export class UserService {
return this.user;
}

const rawData = sessionStorage.getItem('fusio_user');
const rawData = sessionStorage.getItem(this.getKey());
if (!rawData) {
return undefined;
}
Expand All @@ -38,10 +39,13 @@ export class UserService {

public logout(): void {
this.user = undefined;
sessionStorage.removeItem('fusio_user');
sessionStorage.removeItem(this.getKey());
this.fusio.logout();

this.event.dispatchLogout();
}

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

0 comments on commit f4f7558

Please sign in to comment.