Angular-oauth2-oidc: Support for prefix on storage

Created on 14 Nov 2017  路  2Comments  路  Source: manfredsteyer/angular-oauth2-oidc

If you are running two applications on the same host name both applications will share same local storage so it will be nice to have possibility to add prefix to access_token, expires_at and other properties that are saved in storage of the browser so two applications can work each with their own token on the same host.

Most helpful comment

export class MyStorage {

    _storage: Storage;
    _prefix: string = "";

    constructor(storage: Storage, prefix: string = "") {
        super();

        this._storage = storage;
        this._prefix = prefix + "_";
    }

    private makeKey(key: string): string {
        return this._prefix + key;
    }

    getItem(key: string): string | null {
        return this._storage.getItem(this.makeKey(key));
    }

    removeItem(key: string): void {
        this._storage.removeItem(this.makeKey(key));
    }

    setItem(key: string, data: string): void {
        this._storage.setItem(this.makeKey(key), data);
    }
}

All 2 comments

This is currently achievable by specifying your own storage implementation and passing it via OAuthService's setStorage method. In your implementation you can pass a prefix value in the constructor and use it in the getItem, removeItem, setItem methods.

export class MyStorage {

    _storage: Storage;
    _prefix: string = "";

    constructor(storage: Storage, prefix: string = "") {
        super();

        this._storage = storage;
        this._prefix = prefix + "_";
    }

    private makeKey(key: string): string {
        return this._prefix + key;
    }

    getItem(key: string): string | null {
        return this._storage.getItem(this.makeKey(key));
    }

    removeItem(key: string): void {
        this._storage.removeItem(this.makeKey(key));
    }

    setItem(key: string, data: string): void {
        this._storage.setItem(this.makeKey(key), data);
    }
}
Was this page helpful?
0 / 5 - 0 ratings