Skip to content

Commit

Permalink
Change Angular auth oidc clients (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
elanderson authored Aug 1, 2017
1 parent a7222cd commit f7e2a3d
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 253 deletions.
6 changes: 1 addition & 5 deletions ClientApp/ClientApp/app/app.module.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { sharedConfig } from './app.module.shared';

import { AuthService } from './components/services/auth.service';
import { GlobalEventsManager } from './components/services/global.events.manager';
import { AuthGuardService } from './components/services/auth-guard.service';

@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
Expand All @@ -20,7 +16,7 @@ import { AuthGuardService } from './components/services/auth-guard.service';
providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin },
{ provide: 'API_URL', useValue: "http://localhost:5001/api/" },
AuthService, AuthGuardService, GlobalEventsManager
...sharedConfig.providers
]
})
export class AppModule {
Expand Down
6 changes: 1 addition & 5 deletions ClientApp/ClientApp/app/app.module.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { sharedConfig } from './app.module.shared';

import { AuthService } from './components/services/auth.service';
import { GlobalEventsManager } from './components/services/global.events.manager';
import { AuthGuardService } from './components/services/auth-guard.service';

@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
Expand All @@ -14,7 +10,7 @@ import { AuthGuardService } from './components/services/auth-guard.service';
...sharedConfig.imports
],
providers: [
AuthService, AuthGuardService, GlobalEventsManager
...sharedConfig.providers
]
})
export class AppModule {
Expand Down
11 changes: 4 additions & 7 deletions ClientApp/ClientApp/app/app.module.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';
import { CallbackComponent } from './components/callback/callback.component';
import { UnauthorizedComponent } from './components/unauthorized/unauthorized.component';

import { AuthModule } from 'angular-auth-oidc-client';
import { AuthService } from './components/services/auth.service';
import { GlobalEventsManager } from './components/services/global.events.manager';
import { AuthGuardService } from './components/services/auth-guard.service';

export const sharedConfig: NgModule = {
bootstrap: [ AppComponent ],
Expand All @@ -21,19 +19,18 @@ export const sharedConfig: NgModule = {
CounterComponent,
FetchDataComponent,
HomeComponent,
CallbackComponent,
UnauthorizedComponent
],
imports: [
AuthModule.forRoot(),
RouterModule.forRoot([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'callback', component: CallbackComponent },
{ path: 'unauthorized', component: UnauthorizedComponent },
{ path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent, canActivate:[AuthGuardService] },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: '**', redirectTo: 'home' }
])
],
providers: [ AuthService, AuthGuardService, GlobalEventsManager ]
providers: [ AuthService ]
};
13 changes: 0 additions & 13 deletions ClientApp/ClientApp/app/components/callback/callback.component.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { AuthService } from '../services/auth.service';
export class FetchDataComponent {
public forecasts: WeatherForecast[];

constructor(http: Http, @Inject('API_URL') apiUrl: string, authService: AuthService) {
authService.AuthGet(apiUrl + 'SampleData/WeatherForecasts').subscribe(result => {
constructor(authService: AuthService, @Inject('API_URL') apiUrl: string) {
authService.get(apiUrl + 'SampleData/WeatherForecasts').subscribe(result => {
this.forecasts = result.json() as WeatherForecast[];
});
}
Expand Down
24 changes: 7 additions & 17 deletions ClientApp/ClientApp/app/components/navmenu/navmenu.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,19 @@
<div class='navbar-collapse collapse'>
<ul class='nav navbar-nav'>
<li [routerLinkActive]="['link-active']">
<a [routerLink]="['/home']">
<span class='glyphicon glyphicon-home'></span> Home
</a>
<a [routerLink]="['/home']"><span class='glyphicon glyphicon-home'></span> Home</a>
</li>
<li [routerLinkActive]="['link-active']">
<a [routerLink]="['/counter']">
<span class='glyphicon glyphicon-education'></span> Counter
</a>
<a [routerLink]="['/counter']"><span class='glyphicon glyphicon-education'></span> Counter</a>
</li>
<li [routerLinkActive]="['link-active']">
<a [routerLink]="['/fetch-data']">
<span class='glyphicon glyphicon-th-list'></span> Fetch data
</a>
<a *ngIf="isAuthorized" [routerLink]="['/fetch-data']"><span class='glyphicon glyphicon-th-list'></span> Fetch data</a>
</li>
<li *ngIf="!_loggedIn" [routerLinkActive]="['link-active']">
<a (click)="login()" [routerLink]="['/login']">
<span class="glyphicon glyphicon-user"></span> Login
</a>
<li [routerLinkActive]="['link-active']">
<a *ngIf="!isAuthorized" (click)="login()" [routerLink]="['/login']"><span class="glyphicon glyphicon-user"></span> Login</a>
</li>
<li *ngIf="_loggedIn" [routerLinkActive]="['link-active']">
<a (click)="logout()" [routerLink]="['/logout']">
<span class='glyphicon glyphicon-log-out'></span> Logout
</a>
<li [routerLinkActive]="['link-active']">
<a *ngIf="isAuthorized" (click)="logout()"><span class='glyphicon glyphicon-log-out'></span> Logout</a>
</li>
</ul>
</div>
Expand Down
63 changes: 38 additions & 25 deletions ClientApp/ClientApp/app/components/navmenu/navmenu.component.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import { Component } from '@angular/core';
import { AuthService } from '../services/auth.service'
import { GlobalEventsManager } from '../services/global.events.manager'
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import { AuthService } from '../services/auth.service';


@Component({
selector: 'nav-menu',
templateUrl: './navmenu.component.html',
styleUrls: ['./navmenu.component.css']
})
export class NavMenuComponent {
public _loggedIn: boolean = false;

constructor (
private _authService: AuthService,
private _globalEventsManager: GlobalEventsManager) {
_globalEventsManager.showNavBarEmitter.subscribe((mode)=>{
// mode will be null the first time it is created, so you need to igonore it when null
if (mode !== null) {
console.log("Global Event, sent: " + mode);
this._loggedIn = mode;
}
});
}

public login(){
this._authService.startSigninMainWindow();
}

public logout(){
this._authService.startSignoutMainWindow();
}
export class NavMenuComponent implements OnInit, OnDestroy {
isAuthorizedSubscription: Subscription;
isAuthorized: boolean;

constructor(public authService: AuthService) {
}

ngOnInit() {
this.isAuthorizedSubscription = this.authService.getIsAuthorized().subscribe(
(isAuthorized: boolean) => {
this.isAuthorized = isAuthorized;
});

if (window.location.hash) {
this.authService.authorizedCallback();
}
}

ngOnDestroy(): void {
this.isAuthorizedSubscription.unsubscribe();
}

public login() {
this.authService.login();
}

public refreshSession() {
this.authService.refreshSession();
}

public logout() {
this.authService.logout();
}
}
20 changes: 0 additions & 20 deletions ClientApp/ClientApp/app/components/services/auth-guard.service.ts

This file was deleted.

Loading

0 comments on commit f7e2a3d

Please sign in to comment.