Commit 60894709 authored by Eduardo Sanz García's avatar Eduardo Sanz García
Browse files

refactor: WIP forRoot configuration (optional arg)

parent 964ff410
......@@ -13,11 +13,7 @@ import {
} from './modules/auth/auth.module';
export function getToken(): string {
const token = localStorage.getItem('id_token');
if (token === null){
throw Error('Unable to access localStorage token');
}
return token;
return localStorage.getItem('id_token') || '';
}
export function removeToken(): void {
return localStorage.removeItem('id_token');
......@@ -30,18 +26,21 @@ export function updateToken(newToken: string): void {
declarations: [
AppComponent
],
imports: [
BrowserModule,
// AuthModule
AuthModule.forRoot({
aapURL: 'test',
tokenRemover: removeToken,
tokenUpdater: updateToken,
config: {
tokenGetter: getToken,
}
})
AuthModule.forRoot(),
// AuthModule.forRoot({
// aapURL: 'https://api.aai.ebi.ac.uk',
// tokenGetter: getToken,
// tokenUpdater: updateToken,
// tokenRemover: removeToken,
// }),
// JwtModule.forRoot({
// config: {
// tokenGetter: getToken,
// whitelistedDomains: []
// }
// })
],
providers: [
],
......
import {
InjectionToken
} from '@angular/core';
import {
JwtModuleOptions
} from '@auth0/angular-jwt';
export interface AuthConfig extends JwtModuleOptions {
export interface AuthConfig {
aapURL: string;
// tokenGetter: () => string | null;
tokenGetter: () => string;
tokenRemover: () => void;
tokenUpdater: (newToken: any) => void;
}
export const AAP_CONFIG = new InjectionToken < AuthConfig > ('AAP_CONFIG');
export function getToken(): string {
return localStorage.getItem('id_token') || '';
}
export function removeToken(): void {
return localStorage.removeItem('id_token');
}
export function updateToken(newToken: string): void {
return localStorage.setItem('id_token', newToken);
}
export const DEFAULT_CONF: AuthConfig = {
aapURL: 'https://api.aai.ebi.ac.uk',
tokenGetter: getToken,
tokenRemover: removeToken,
tokenUpdater: updateToken
};
......@@ -4,66 +4,24 @@ import {
SkipSelf,
ModuleWithProviders,
} from '@angular/core';
import {
CommonModule
} from '@angular/common';
import {
AuthConfig,
AAP_CONFIG
AAP_CONFIG,
DEFAULT_CONF
} from './auth.config';
import {
AuthService
} from './auth.service';
import {
JwtModule,
JWT_OPTIONS,
JwtHelperService
} from '@auth0/angular-jwt';
export function getToken(): string {
const token = localStorage.getItem('id_token');
if (token === null) {
throw Error('Unable to access localStorage token');
}
return token;
}
export function removeToken(): void {
return localStorage.removeItem('id_token');
}
export function updateToken(newToken: string): void {
return localStorage.setItem('id_token', newToken);
}
@NgModule({
imports: [
CommonModule,
// JwtModule.forRoot({
// config: {
// tokenGetter: getToken,
// whitelistedDomains: []
// }
// })
],
providers: [{
provide: AAP_CONFIG,
useValue: {
aapURL: 'https://api.aai.ebi.ac.uk',
tokenRemover: removeToken,
tokenUpdater: updateToken,
@NgModule({})
export class AuthModule {
}
},
AuthService,
{
provide: JWT_OPTIONS,
useValue: {
tokenGetter: getToken,
}
},
JwtHelperService
]
}) export class AuthModule {
constructor(@Optional() @SkipSelf() parentModule: AuthModule) {
if (parentModule) {
throw new Error('AuthModule is already loaded. It should only be imported in your application\'s main module.');
......@@ -71,46 +29,20 @@ export function updateToken(newToken: string): void {
}
static forRoot(options?: AuthConfig): ModuleWithProviders {
const tokenName = 'id_token';
const defaultConf: AuthConfig = {
aapURL: 'https://api.aai.ebi.ac.uk',
tokenRemover: () => localStorage.removeItem(tokenName),
tokenUpdater: (newToken: any) => localStorage.setItem(tokenName, newToken),
config: {
tokenGetter: () => {
return localStorage.getItem(tokenName) || '';
}
}
};
if (options && options.config && options.config.tokenGetter) {
return {
ngModule: AuthModule,
providers: [{
provide: AAP_CONFIG,
useValue: options.config
},
AuthService,
{
provide: JWT_OPTIONS,
useValue: options.config
},
JwtHelperService
]
};
}
return {
ngModule: AuthModule,
providers: [{
provide: AAP_CONFIG,
useValue: defaultConf
useValue: options ? options : DEFAULT_CONF
},
AuthService,
{
provide: JWT_OPTIONS,
useValue: defaultConf.config
useValue: options ? {
tokenGetter: options.tokenGetter
} : {
tokenGetter: DEFAULT_CONF.tokenGetter
}
},
JwtHelperService
]
......
......@@ -178,12 +178,12 @@
"@types/jasmine" "*"
"@types/node@^6.0.46":
version "6.0.96"
resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.96.tgz#7bf0bf40d6ce51e93762cc47d010c8cc5ebb2179"
version "6.0.97"
resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.97.tgz#72199848e8f3cfa975864031ac12e0ef6ccfe054"
"@types/node@~9.4.0":
version "9.4.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.0.tgz#b85a0bcf1e1cc84eb4901b7e96966aedc6f078d1"
version "9.4.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.1.tgz#0f636f7837e15d2d73a7f6f3ea0e322eb2a5ab65"
"@types/q@^0.0.32":
version "0.0.32"
......@@ -850,8 +850,8 @@ brorand@^1.0.1:
resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f"
browser-pack@^6.0.1:
version "6.0.3"
resolved "https://registry.yarnpkg.com/browser-pack/-/browser-pack-6.0.3.tgz#91ca96518583ef580ab063a309de62e407767a39"
version "6.0.4"
resolved "https://registry.yarnpkg.com/browser-pack/-/browser-pack-6.0.4.tgz#9a73beb3b48f9e36868be007b64400102c04a99f"
dependencies:
JSONStream "^1.0.3"
combine-source-map "~0.8.0"
......@@ -1119,12 +1119,12 @@ caniuse-api@^1.5.2:
lodash.uniq "^4.5.0"
caniuse-db@^1.0.30000529, caniuse-db@^1.0.30000634, caniuse-db@^1.0.30000639:
version "1.0.30000803"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000803.tgz#3e8d2baf56c2fd5a59c82e227928a0dc2c26702d"
version "1.0.30000804"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000804.tgz#84feb42018fc64cf6aff6371e43115f292c00179"
caniuse-lite@^1.0.30000791, caniuse-lite@^1.0.30000792:
version "1.0.30000803"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000803.tgz#9939c37149d38d5f4540430490d240c03106a0f5"
version "1.0.30000804"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000804.tgz#8729a143d65378e8936adbb161f550e9c49fc09d"
caseless@~0.11.0:
version "0.11.0"
......@@ -1408,8 +1408,8 @@ commander@2.12.x:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.12.2.tgz#0f5946c427ed9ec0d91a46bb9def53e54650e555"
commander@^2.11.0, commander@^2.12.1, commander@^2.9.0:
version "2.14.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.14.0.tgz#7b25325963e6aace20d3a9285b09379b0c2208b5"
version "2.14.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.14.1.tgz#2235123e37af8ca3c65df45b026dbd357b01b9aa"
commander@~2.13.0:
version "2.13.0"
......@@ -2133,8 +2133,8 @@ ejs@^2.5.7:
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.5.7.tgz#cc872c168880ae3c7189762fd5ffc00896c9518a"
electron-to-chromium@^1.2.7, electron-to-chromium@^1.3.30:
version "1.3.32"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.32.tgz#11d0684c0840e003c4be8928f8ac5f35dbc2b4e6"
version "1.3.33"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.33.tgz#bf00703d62a7c65238136578c352d6c5c042a545"
elliptic@^6.0.0:
version "6.4.0"
......@@ -7022,10 +7022,10 @@ sshpk@^1.7.0:
tweetnacl "~0.14.0"
ssri@^5.0.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/ssri/-/ssri-5.1.0.tgz#2cbf1df36b74d0fc91fcf89640a4b3e1d10b1899"
version "5.2.1"
resolved "https://registry.yarnpkg.com/ssri/-/ssri-5.2.1.tgz#8b6eb873688759bd3c75a88dee74593d179bb73c"
dependencies:
safe-buffer "^5.1.0"
safe-buffer "^5.1.1"
static-extend@^0.1.1:
version "0.1.2"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment