Skip to content

Commit

Permalink
feat(storage): add storage service
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Jun 8, 2020
1 parent 9902593 commit 25c033d
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/auth/src/lib/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
MatButtonModule
} from '@angular/material';

import { IgoLanguageModule } from '@igo2/core';
import { IgoLanguageModule, StorageService } from '@igo2/core';

import { AuthStorageService } from './shared/storage.service';
import { ProtectedDirective } from './shared/protected.directive';
import { AuthInterceptor } from './shared/auth.interceptor';

Expand Down Expand Up @@ -45,6 +46,10 @@ export class IgoAuthModule {
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
},
{
provide: StorageService,
useClass: AuthStorageService
}
]
};
Expand Down
2 changes: 2 additions & 0 deletions packages/auth/src/lib/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export * from './auth.guard';
export * from './admin.guard';
export * from './profils.guard';
export * from './protected.directive';
export * from './storage.interface';
export * from './storage.service';
5 changes: 5 additions & 0 deletions packages/auth/src/lib/shared/storage.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { StorageOptions } from '@igo2/core';

export interface AuthStorageOptions extends StorageOptions {
url: string;
}
66 changes: 66 additions & 0 deletions packages/auth/src/lib/shared/storage.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { StorageService, StorageScope, ConfigService } from '@igo2/core';
import { AuthService } from './auth.service';
import { AuthStorageOptions } from './storage.interface';

@Injectable({
providedIn: 'root'
})
export class AuthStorageService extends StorageService {
protected options: AuthStorageOptions;

constructor(
config: ConfigService,
private http: HttpClient,
private authService: AuthService
) {
super(config);

this.authService.authenticate$.subscribe(isAuthenticated => {
if (isAuthenticated && this.options.url) {
this.http
.get(this.options.url)
.subscribe((userIgo: { preference: object }) => {
if (userIgo && userIgo.preference) {
for (const key of Object.keys(userIgo.preference)) {
const value = userIgo.preference[key];
super.set(key, value);
}
}
});
}
});
}

set(
key: string,
value: string | object,
scope: StorageScope = StorageScope.LOCAL
) {
if (
scope === StorageScope.LOCAL &&
this.authService.authenticated &&
this.options.url
) {
const preference = {};
preference[key] = value;
this.http.patch(this.options.url, { preference }).subscribe();
}
super.set(key, value, scope);
}

remove(key: string, scope: StorageScope = StorageScope.LOCAL) {
if (
scope === StorageScope.LOCAL &&
this.authService.authenticated &&
this.options.url
) {
const preference = {};
preference[key] = undefined;
this.http.patch(this.options.url, { preference }).subscribe();
}
return super.remove(key, scope);
}
}
2 changes: 2 additions & 0 deletions packages/auth/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export * from './lib/shared/auth.interceptor';
export * from './lib/shared/auth.interface';
export * from './lib/shared/protected.directive';
export * from './lib/shared/token.service';
export * from './lib/shared/storage.interface';
export * from './lib/shared/storage.service';
export * from './lib/auth-routing.module';
export * from './lib/auth.module';
2 changes: 2 additions & 0 deletions packages/core/src/lib/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './storage.service';
export * from './storage.interface';
8 changes: 8 additions & 0 deletions packages/core/src/lib/storage/storage.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum StorageScope {
SESSION = 'Session',
LOCAL = 'Local'
}

export interface StorageOptions {
key: string;
}
62 changes: 62 additions & 0 deletions packages/core/src/lib/storage/storage.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Injectable } from '@angular/core';

import { ConfigService } from '../config/config.service';
import { StorageScope, StorageOptions } from './storage.interface';

@Injectable({
providedIn: 'root'
})
export class StorageService {
protected options: StorageOptions;

constructor(private config: ConfigService) {
this.options = this.config.getConfig('storage') || { key: 'igo' };
}
/**
* Use to get the data found in storage file
*/
get(key: string, scope?: StorageScope) {
let value: string;

if (!scope || scope === StorageScope.SESSION) {
value = sessionStorage.getItem(`${this.options.key}.${key}`);
}

if (scope === StorageScope.LOCAL || (!value && !scope)) {
value = localStorage.getItem(`${this.options.key}.${key}`);
}

if (value) {
try {
value = JSON.parse(value);
} catch {
value = value;
}
}

return value;
}

set(
key: string,
value: string | object,
scope: StorageScope = StorageScope.LOCAL
) {
if (scope === StorageScope.SESSION) {
sessionStorage.setItem(
`${this.options.key}.${key}`,
JSON.stringify(value)
);
} else {
localStorage.setItem(`${this.options.key}.${key}`, JSON.stringify(value));
}
}

remove(key: string, scope: StorageScope = StorageScope.LOCAL) {
if (scope === StorageScope.SESSION) {
sessionStorage.removeItem(`${this.options.key}.${key}`);
} else {
localStorage.removeItem(`${this.options.key}.${key}`);
}
}
}
1 change: 1 addition & 0 deletions packages/core/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export * from './lib/language';
export * from './lib/media';
export * from './lib/message';
export * from './lib/request';
export * from './lib/storage';
export * from './lib/network';
8 changes: 5 additions & 3 deletions packages/utils/src/lib/user-agent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as Bowser from "bowser";
import * as Bowser from 'bowser';

export interface UserAgent extends Bowser.Parser.Parser {
compareVersion: (version: string) => boolean;
};
}

export const userAgent: UserAgent = (Bowser.getParser(window.navigator.userAgent) as any);
export const userAgent: UserAgent = Bowser.getParser(
window.navigator.userAgent
) as any;

0 comments on commit 25c033d

Please sign in to comment.