Skip to content

Commit

Permalink
feat(context): use api to retrieve contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Aug 7, 2017
1 parent 57b3884 commit fbe6d10
Show file tree
Hide file tree
Showing 25 changed files with 322 additions and 96 deletions.
1 change: 0 additions & 1 deletion src/demo-app/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<igo-spinner igoSpinnerBinding></igo-spinner>
<igo-message-center></igo-message-center>
<igo-auth-form></igo-auth-form>
<md-sidenav-container>

<md-sidenav
Expand Down
21 changes: 8 additions & 13 deletions src/demo-app/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { SearchSourcesOptions, LanguageOptions, AuthOptions } from '../../lib';
// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.

import { SearchSourcesOptions, LanguageOptions, AuthOptions,
ContextServiceOptions } from '../../lib';

interface Environment {
production: boolean;
igo: {
searchSources?: SearchSourcesOptions;
language?: LanguageOptions;
auth?: AuthOptions;
context?: ContextServiceOptions;
};
};

Expand All @@ -19,18 +26,6 @@ export const environment: Environment = {
},
language: {
prefix: './assets/locale/'
},
auth: {
url: 'http://localhost:8000/users',
tokenKey: 'id_token_igo',
google: {
apiKey: 'AIzaSyCbc-E35ZNqAjPvpbr30bAXwfcQoq5XLBs',
clientId: '467961599657-f7lebhfn3oposibnrvlgjl7ffglgr2go.apps.googleusercontent.com'
},
facebook: {
apiKey: '1989457734616371',
enabled: false
}
}
}
};
16 changes: 3 additions & 13 deletions src/demo-app/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.

import { SearchSourcesOptions, LanguageOptions, AuthOptions } from '../../lib';
import { SearchSourcesOptions, LanguageOptions, AuthOptions,
ContextServiceOptions } from '../../lib';

interface Environment {
production: boolean;
igo: {
searchSources?: SearchSourcesOptions;
language?: LanguageOptions;
auth?: AuthOptions;
context?: ContextServiceOptions;
};
};

Expand All @@ -24,18 +26,6 @@ export const environment: Environment = {
},
language: {
prefix: './assets/locale/'
},
auth: {
url: 'http://localhost:8000/users',
tokenKey: 'id_token_igo',
google: {
apiKey: 'AIzaSyCbc-E35ZNqAjPvpbr30bAXwfcQoq5XLBs',
clientId: '467961599657-f7lebhfn3oposibnrvlgjl7ffglgr2go.apps.googleusercontent.com'
},
facebook: {
apiKey: '1989457734616371',
enabled: false
}
}
}
};
6 changes: 5 additions & 1 deletion src/lib/auth/auth-form/auth-facebook.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, ChangeDetectionStrategy, ApplicationRef } from '@angular/core';
import { Component, ChangeDetectionStrategy, ApplicationRef,
Output, EventEmitter } from '@angular/core';

import { ConfigService } from '../../core';
import { AuthService, AuthFacebookOptions } from '../shared';
Expand All @@ -13,6 +14,8 @@ export class AuthFacebookComponent {

private options: AuthFacebookOptions;

@Output() onLogin: EventEmitter<boolean> = new EventEmitter<boolean>();

constructor(private authService: AuthService,
private config: ConfigService,
private appRef: ApplicationRef) {
Expand Down Expand Up @@ -40,6 +43,7 @@ export class AuthFacebookComponent {
private login(token) {
this.authService.loginWithToken(token, 'facebook').subscribe(() => {
this.appRef.tick();
this.onLogin.emit(true);
});
}

Expand Down
22 changes: 18 additions & 4 deletions src/lib/auth/auth-form/auth-form.component.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
<div *ngIf="!auth.logged" class="backgroundDisable"></div>
<div *ngIf="!auth.logged && backgroundDisable" class="backgroundDisable"></div>

<div *ngIf="!auth.logged" class="login center-block">
<h1>{{'igo.auth.connection' | translate}}</h1>

<igo-auth-google *ngIf="options.google && options.google.enabled !== false"></igo-auth-google>
<igo-auth-facebook *ngIf="options.facebook && options.facebook.enabled !== false"></igo-auth-facebook>
<igo-auth-intern *ngIf="!options.intern || options.intern.enabled !== false"></igo-auth-intern>
<igo-auth-google
*ngIf="options.google && options.google.enabled !== false"
(onLogin)="login()">
</igo-auth-google>
<igo-auth-facebook
*ngIf="options.facebook && options.facebook.enabled !== false"
(onLogin)="login()">
</igo-auth-facebook>
<igo-auth-intern
*ngIf="!options.intern || options.intern.enabled !== false"
(onLogin)="login()">
</igo-auth-intern>
</div>

<div *ngIf="auth.logged && alreadyConnectedDiv" class="login center-block">
<p>{{'igo.auth.welcome' | translate: user}}</p>
<button (click)="logout()">{{'igo.auth.signOut' | translate}}</button>
</div>
67 changes: 64 additions & 3 deletions src/lib/auth/auth-form/auth-form.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { Component, ChangeDetectionStrategy,
OnInit, Input } from '@angular/core';
import { Router } from '@angular/router';

import { ConfigService } from '../../core';
import { AuthService, AuthOptions } from "../shared";
Expand All @@ -9,13 +11,72 @@ import { AuthService, AuthOptions } from "../shared";
styleUrls: ['./auth-form.component.styl'],
changeDetection: ChangeDetectionStrategy.Default
})
export class AuthFormComponent {
export class AuthFormComponent implements OnInit {

@Input()
get alreadyConnectedDiv(): boolean {
return this._alreadyConnectedDiv;
}
set alreadyConnectedDiv(value: boolean ) {
this._alreadyConnectedDiv = value.toString() === 'true';
}
private _alreadyConnectedDiv: boolean = false;

@Input()
get backgroundDisable(): boolean {
return this._backgroundDisable;
}
set backgroundDisable(value: boolean) {
this._backgroundDisable = value.toString() === 'true';
}
private _backgroundDisable: boolean = true;

private options: AuthOptions;
private user;

constructor(
public auth: AuthService,
private config: ConfigService
private config: ConfigService,
private router: Router
) {
this.options = this.config.getConfig('auth') || {};

if (this.auth.decodeToken()) {
this.user = {
name: this.auth.decodeToken().user.sourceId
}
}
}

public ngOnInit() {
this.analyzeRoute();
}

protected login() {
this.auth.goToRedirectUrl();
}

protected logout() {
this.auth.logout().subscribe(() => {
if (this.options.loginRoute) {
this.router.navigate([this.options.loginRoute]);
}
});
}

private analyzeRoute() {
const logoutRoute = this.options.logoutRoute;
const loginRoute = this.options.loginRoute;
const currentRoute = this.router.url;

const isLogoutRoute: boolean = currentRoute === logoutRoute;
const isLoginRoute: boolean = currentRoute === loginRoute;

if (isLogoutRoute) {
this.logout();
} else if (isLoginRoute) {
this.backgroundDisable = false;
this.alreadyConnectedDiv = true;
}
}
}
6 changes: 5 additions & 1 deletion src/lib/auth/auth-form/auth-google.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, ChangeDetectionStrategy, ApplicationRef } from '@angular/core';
import { Component, ChangeDetectionStrategy, ApplicationRef,
Output, EventEmitter } from '@angular/core';

import { ConfigService } from '../../core';
import { AuthService, AuthGoogleOptions } from '../shared';
Expand All @@ -13,6 +14,8 @@ export class AuthGoogleComponent {

private options: AuthGoogleOptions;

@Output() onLogin: EventEmitter<boolean> = new EventEmitter<boolean>();

constructor(private authService: AuthService,
private config: ConfigService,
private appRef: ApplicationRef) {
Expand Down Expand Up @@ -60,6 +63,7 @@ export class AuthGoogleComponent {
private login(token) {
this.authService.loginWithToken(token, 'google').subscribe(() => {
this.appRef.tick();
this.onLogin.emit(true);
});
}

Expand Down
10 changes: 7 additions & 3 deletions src/lib/auth/auth-form/auth-intern.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { Component, ChangeDetectionStrategy,
Output, EventEmitter } from '@angular/core';
import { Validators, FormGroup, FormBuilder } from "@angular/forms";
import { AuthService } from "../shared/auth.service";

Expand All @@ -12,6 +13,8 @@ export class AuthInternComponent {
public error: string = "";
private form: FormGroup;

@Output() onLogin: EventEmitter<boolean> = new EventEmitter<boolean>();

constructor(
public auth: AuthService,
fb: FormBuilder
Expand All @@ -22,11 +25,12 @@ export class AuthInternComponent {
});
}


protected login(values: any) {
this.auth.login(values.username, values.password)
.subscribe(
() => {},
() => {
this.onLogin.emit(true);
},
(errors: any) => {
let message = "";
for (let err in errors) {
Expand Down
16 changes: 16 additions & 0 deletions src/lib/auth/auth-routing.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AuthFormComponent } from './auth-form';

const routes: Routes = [
{ path: 'login', component: AuthFormComponent },
{ path: 'logout', component: AuthFormComponent }
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
providers: []
})
export class AuthRoutingModule { }
1 change: 1 addition & 0 deletions src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { AuthHttp } from 'angular2-jwt';
export * from './shared';
export * from './auth-form';
export * from './module';
export * from './auth-routing.module';
30 changes: 14 additions & 16 deletions src/lib/auth/module.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';

import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { AuthHttp } from 'angular2-jwt';

import { ConfigService } from '../core';
import { IgoSharedModule } from '../shared';
import { AuthFormComponent,
AuthInternComponent,
AuthFacebookComponent,
AuthGoogleComponent
} from './auth-form';

import { AuthService } from './shared';


export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig({
headerName: 'Authorization',
headerPrefix: '',
tokenName: 'id_token_igo', // TODO : move in config
tokenGetter: (() => localStorage.getItem('id_token_igo')),
noJwtError: true
}), http, options);
}
import { AuthService,
AuthGuard,
ProtectedDirective,
authHttpServiceFactory } from './shared';

@NgModule({
imports: [IgoSharedModule],
declarations: [
AuthFormComponent,
AuthInternComponent,
AuthFacebookComponent,
AuthGoogleComponent
AuthGoogleComponent,
ProtectedDirective
],
exports: [AuthFormComponent]
exports: [
AuthFormComponent,
ProtectedDirective
]
})


Expand All @@ -41,10 +38,11 @@ export class IgoAuthModule {
ngModule: IgoAuthModule,
providers: [
AuthService,
AuthGuard,
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions]
deps: [Http, RequestOptions, ConfigService]
}
]
};
Expand Down
18 changes: 18 additions & 0 deletions src/lib/auth/shared/auth-http.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Http, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig } from 'angular2-jwt';

import { ConfigService } from '../../core';

export function authHttpServiceFactory(http: Http, options: RequestOptions,
config: ConfigService) {

const authConfig = config.getConfig('auth') || {};

return new AuthHttp(new AuthConfig({
headerName: 'Authorization',
headerPrefix: '',
tokenName: authConfig.tokenKey,
tokenGetter: (() => localStorage.getItem(authConfig.tokenKey)),
noJwtError: true
}), http, options);
}
Loading

0 comments on commit fbe6d10

Please sign in to comment.