-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './storage.service'; | ||
export * from './storage.interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |