Skip to content

Commit

Permalink
style(project-wide): lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Burak Tasci committed Dec 24, 2018
1 parent e601431 commit d8fa0d1
Show file tree
Hide file tree
Showing 29 changed files with 628 additions and 893 deletions.
28 changes: 13 additions & 15 deletions packages/@ngx-auth/auth0/index.ts
@@ -1,20 +1,17 @@
// angular
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';

// libs
import { AuthGuard, AuthLoader, AuthServerGuard, AuthService } from '@ngx-auth/core';

// module
import { Auth0ServerService } from './src/auth0-server.service';
import { Auth0StaticLoader } from './src/auth0.loader';
import { Auth0Service } from './src/auth0.service';
import { Auth0ServerService } from './src/auth0-server.service';

export * from './src/models/auth0-backend';
export * from './src/models/auth0-settings';
export * from './src/auth0.loader';
export * from './src/auth0.service';

// for AoT compilation
// tslint:disable-next-line
export function auth0Factory(): AuthLoader {
return new Auth0StaticLoader();
}
Expand All @@ -28,20 +25,20 @@ export function auth0Factory(): AuthLoader {
},
{
provide: AuthLoader,
useFactory: (auth0Factory)
useFactory: auth0Factory
}
]
})
export class Auth0Module {
static forRoot(configuredProvider: any = {
provide: AuthLoader,
useFactory: (auth0Factory)
}): ModuleWithProviders {
static forRoot(
configuredProvider: any = {
provide: AuthLoader,
useFactory: auth0Factory
}
): ModuleWithProviders {
return {
ngModule: Auth0Module,
providers: [
configuredProvider
]
providers: [configuredProvider]
};
}

Expand All @@ -61,8 +58,9 @@ export class Auth0Module {
};
}

constructor(@Optional() @SkipSelf() parentModule: Auth0Module) {
if (parentModule)
constructor(@Optional() @SkipSelf() parentModule?: Auth0Module) {
if (parentModule) {
throw new Error('Auth0Module already loaded; import in root module only.');
}
}
}
3 changes: 0 additions & 3 deletions packages/@ngx-auth/auth0/src/auth0-server.service.ts
@@ -1,7 +1,4 @@
// angular
import { Injectable } from '@angular/core';

// libs
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';

@Injectable()
Expand Down
29 changes: 14 additions & 15 deletions packages/@ngx-auth/auth0/src/auth0.loader.ts
@@ -1,7 +1,5 @@
// libs
import { AuthLoader } from '@ngx-auth/core';

// module
import { Auth0Backend } from './models/auth0-backend';
import { Auth0Settings } from './models/auth0-settings';

Expand Down Expand Up @@ -30,17 +28,18 @@ export class Auth0StaticLoader implements AuthLoader {
return this.providedSettings.defaultUrl;
}

constructor(private readonly providedSettings: Auth0Settings = {
backend: {
domain: '',
clientID: '',
scope: 'openid',
responseType: 'token id_token'
},
storage: localStorage,
storageKey: 'currentUser',
publicRoute: ['public'],
defaultUrl: ''
}) {
}
constructor(
private readonly providedSettings: Auth0Settings = {
backend: {
domain: '',
clientID: '',
scope: 'openid',
responseType: 'token id_token'
},
storage: localStorage,
storageKey: 'currentUser',
publicRoute: ['public'],
defaultUrl: ''
}
) {}
}
42 changes: 22 additions & 20 deletions packages/@ngx-auth/auth0/src/auth0.service.ts
@@ -1,19 +1,16 @@
// angular
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

// libs
import { BehaviorSubject, from as observableFrom, Observable } from 'rxjs';
import * as auth0js from 'auth0-js';
import { AuthLoader } from '@ngx-auth/core';
import * as auth0js from 'auth0-js';
import { BehaviorSubject, from as observableFrom, Observable } from 'rxjs';

@Injectable()
export class Auth0Service {
private auth0: any;
private readonly auth0: any;
private _accessToken: string;
private _idToken: string;
private _expiresAt: string;
private _isAuthenticated$: BehaviorSubject<boolean>;
private readonly _isAuthenticated$: BehaviorSubject<boolean>;

get accessToken(): string {
return this._accessToken;
Expand All @@ -32,7 +29,9 @@ export class Auth0Service {
}

get isAuthenticated(): boolean {
const expiresAt = JSON.parse(this.loader.storage.getItem(this.loader.storageKey) || '{}').expiresAt;
const stored = this.loader.storage.getItem(this.loader.storageKey);
const authContent = stored ? stored : '{}';
const expiresAt = JSON.parse(authContent).expiresAt;

return new Date().getTime() < expiresAt;
}
Expand All @@ -41,11 +40,12 @@ export class Auth0Service {
return this._isAuthenticated$;
}

constructor(readonly loader: AuthLoader,
protected readonly router: Router) {
constructor(readonly loader: AuthLoader, protected readonly router: Router) {
this.auth0 = new auth0js.WebAuth(this.loader.backend);

const current = JSON.parse(this.loader.storage.getItem(this.loader.storageKey) || '{}');
const stored = this.loader.storage.getItem(this.loader.storageKey);
const authContent = stored ? stored : '{}';
const current = JSON.parse(authContent);

this._accessToken = current && current.accessToken;
this._idToken = current && current.idToken;
Expand All @@ -59,25 +59,27 @@ export class Auth0Service {
}

authenticate(): Observable<boolean> {
const res$ = new Promise<boolean>(resolve => {
const res$ = new Promise<boolean>((resolve: Function) => {
this.auth0.parseHash((err: any, res: any) => {
if (res && res.accessToken && res.idToken) {
this._accessToken = res.accessToken;
this._idToken = res.idToken;
this._expiresAt = JSON.stringify((res.expiresIn * 1000) + new Date().getTime());
this._expiresAt = JSON.stringify(res.expiresIn * 1000 + new Date().getTime());

this.loader.storage.setItem(this.loader.storageKey,
this.loader.storage.setItem(
this.loader.storageKey,
JSON.stringify({
accessToken: this._accessToken,
idToken: this._idToken,
expiresAt: this._expiresAt
}));
})
);

this.isAuthenticated$.next(true);

this.router.navigateByUrl(this.defaultUrl);

return resolve(true);
return this.router.navigateByUrl(this.defaultUrl).then(() => {
resolve(true);
});
}

return resolve(false);
Expand All @@ -87,7 +89,7 @@ export class Auth0Service {
return observableFrom<boolean>(res$);
}

invalidate(): void {
async invalidate(): Promise<boolean> {
this._accessToken = undefined;
this._idToken = undefined;
this._expiresAt = undefined;
Expand All @@ -96,6 +98,6 @@ export class Auth0Service {

this.isAuthenticated$.next(false);

this.router.navigateByUrl(this.defaultUrl);
return this.router.navigateByUrl(this.defaultUrl);
}
}
1 change: 0 additions & 1 deletion packages/@ngx-auth/auth0/src/models/auth0-settings.ts
@@ -1,4 +1,3 @@
// module
import { Auth0Backend } from './auth0-backend';

export interface Auth0Settings {
Expand Down
23 changes: 13 additions & 10 deletions packages/@ngx-auth/auth0/tests/__mocks__/auth0-js.ts
@@ -1,19 +1,22 @@
let order = 0;

export class WebAuth {
constructor(...args) {/**/}

authorize(): void {/**/}
authorize(): void {
/**/
}

parseHash(callback: Function): void {
order++;

return callback(undefined, order !== 1
? {}
: {
accessToken: 'test',
idToken: 'test',
expiresIn: 7200
});
return callback(
undefined,
order !== 1
? {}
: {
accessToken: 'test',
idToken: 'test',
expiresIn: 7200
}
);
}
}
91 changes: 35 additions & 56 deletions packages/@ngx-auth/auth0/tests/auth0-server.service.spec.ts
@@ -1,73 +1,52 @@
// angular
import { async, inject } from '@angular/core/testing';

// module
import { Auth0Service } from '../index';
import { Auth0ServerService } from '../src/auth0-server.service';

import { testServerModuleConfig } from './common';

describe('@ngx-auth/auth0:',
() => {
beforeEach(testServerModuleConfig);
describe('@ngx-auth/auth0:', () => {
beforeEach(testServerModuleConfig);

describe('Auth0ServerService',
() => {
beforeEach(inject([Auth0Service],
(auth: Auth0Service) => {
auth.invalidate();
}));
describe('Auth0ServerService', () => {
beforeEach(inject([Auth0Service], async (auth: Auth0Service) => auth.invalidate()));

it('is defined',
inject([Auth0Service],
(auth: Auth0Service) => {
auth.authorize();
it('is defined', inject([Auth0Service], (auth: Auth0Service) => {
auth.authorize();

expect(Auth0Service)
.toBeDefined();
expect(auth)
.toBeDefined();
expect(auth instanceof Auth0ServerService)
.toBeTruthy();
}));
expect(Auth0Service).toBeDefined();
expect(auth).toBeDefined();
expect(auth instanceof Auth0ServerService).toBeTruthy();
}));

it('should be able to return an undefined `defaultUrl`',
inject([Auth0Service],
(auth: Auth0Service) => {
const res = auth.defaultUrl;
expect(res)
.toBeUndefined();
}));
it('should be able to return an undefined `defaultUrl`', inject([Auth0Service], (auth: Auth0Service) => {
const res = auth.defaultUrl;
expect(res).toBeUndefined();
}));

it('should not authenticate',
async(inject([Auth0Service],
(auth: Auth0Service) => {
auth.authenticate()
.subscribe(res => {
expect(res)
.toEqual(false);
it('should not authenticate', async(
inject([Auth0Service], (auth: Auth0Service) => {
auth.authenticate().subscribe(res => {
expect(res).toEqual(false);

const accessToken = auth.accessToken;
expect(accessToken)
.toBeUndefined();
const accessToken = auth.accessToken;
expect(accessToken).toBeUndefined();

const idToken = auth.idToken;
expect(idToken)
.toBeUndefined();
const idToken = auth.idToken;
expect(idToken).toBeUndefined();

const expiresAt = auth.expiresAt;
expect(expiresAt)
.toBeUndefined();
const expiresAt = auth.expiresAt;
expect(expiresAt).toBeUndefined();

const isAuthenticated = auth.isAuthenticated;
expect(isAuthenticated)
.toEqual(false);
const isAuthenticated = auth.isAuthenticated;
expect(isAuthenticated).toEqual(false);

const isAuthenticated$ = auth.isAuthenticated$;
isAuthenticated$.subscribe(isAuth => {
expect(isAuth)
.toEqual(false);
});
});
})));
});
const isAuthenticated$ = auth.isAuthenticated$;
isAuthenticated$.subscribe(isAuth => {
expect(isAuth).toEqual(false);
});
});
})
));
});
});

0 comments on commit d8fa0d1

Please sign in to comment.