Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 33e6193

Browse files
committed
Add sample backend service ValueService
1 parent 7d9fc46 commit 33e6193

File tree

10 files changed

+137
-13
lines changed

10 files changed

+137
-13
lines changed

src/app/app.constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {
2+
OpaqueToken
3+
}
4+
from '@angular/core';
5+
6+
export const BaseEndpoint = new OpaqueToken('BaseEndpoint');

src/app/app.module.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { OAuthCallbackHandler } from './login-callback/oauth-callback.guard';
2-
import { OAuthCallbackComponent } from './login-callback/oauth-callback.component';
3-
import { OAuthHandshakeModule } from './login-callback/oauth-callback.module';
4-
import { SharedServicesModule } from './services/shared.services.module';
5-
import { NgModule } from '@angular/core'
1+
import { ValueService } from './services/value.service';
2+
import { ValueComponent } from './values/value.controller';
3+
import { NgModule, APP_INITIALIZER } from '@angular/core'
64
import { RouterModule } from '@angular/router';
75
import { rootRouterConfig } from './app.routes';
86
import { AppComponent } from './app.component';
@@ -19,6 +17,12 @@ import { RepoDetailComponent } from './github/repo-detail/repo-detail.component'
1917
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
2018
import { ContactComponent } from './contact/contact.component';
2119
import { LoginComponent } from './login/login.component';
20+
import { BaseEndpoint } from './app.constants';
21+
import { OAuthCallbackHandler } from './login-callback/oauth-callback.guard';
22+
import { OAuthCallbackComponent } from './login-callback/oauth-callback.component';
23+
import { OAuthHandshakeModule } from './login-callback/oauth-callback.module';
24+
import { SharedServicesModule } from './services/shared.services.module';
25+
2226

2327
@NgModule({
2428
declarations: [
@@ -29,7 +33,8 @@ import { LoginComponent } from './login/login.component';
2933
RepoDetailComponent,
3034
HomeComponent,
3135
ContactComponent,
32-
LoginComponent
36+
LoginComponent,
37+
ValueComponent
3338
],
3439
imports: [
3540
BrowserModule,
@@ -41,9 +46,23 @@ import { LoginComponent } from './login/login.component';
4146
RouterModule.forRoot(rootRouterConfig, { useHash: true })
4247
],
4348
providers: [
44-
GithubService
49+
GithubService,
50+
/*
51+
{
52+
provide: APP_INITIALIZER,
53+
useFactory: () => {
54+
// any app initialize code
55+
},
56+
57+
deps: [],
58+
multi: true
59+
},
60+
*/
61+
{ provide: BaseEndpoint, useValue: 'http://localhost:52233' }, // for asp.net core backend
62+
//{ provide: BaseEndpoint, useValue: 'http://localhost:64897'}, // use asp.net mvc 5 backend
63+
ValueService
4564
],
46-
bootstrap: [ AppComponent ]
65+
bootstrap: [AppComponent]
4766
})
4867
export class AppModule {
4968

src/app/app.routes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ValueComponent } from './values/value.controller';
2+
import { Component } from '@angular/core';
13

24
import { Routes } from '@angular/router';
35

@@ -18,6 +20,7 @@ export const rootRouterConfig: Routes = [
1820
{ path: 'about', component: AboutComponent },
1921
{ path: 'login', component: LoginComponent },
2022
{ path: 'id_token', component: OAuthCallbackComponent, canActivate: [OAuthCallbackHandler] },
23+
{ path: 'values', component: ValueComponent, canActivate: [AuthenticationGuard] },
2124
{
2225
path: 'github', component: RepoBrowserComponent, canActivate: [AuthenticationGuard],
2326
children: [

src/app/services/adal.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class AdalService {
88

99
private context: adal.AuthenticationContext;
1010
constructor(private configService: ConfigService) {
11-
this.context = new createAuthContextFn(configService.getAdalConfig);
11+
this.context = new createAuthContextFn(configService.AdalConfig);
1212
}
1313

1414
login() {
@@ -28,7 +28,7 @@ export class AdalService {
2828
}
2929

3030
public get accessToken() {
31-
return this.context.getCachedToken(this.configService.getAdalConfig.clientId);
31+
return this.context.getCachedToken(this.configService.AdalConfig.clientId);
3232
}
3333

3434
public get isAuthenticated() {

src/app/services/base.service.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Http, Response, Headers } from '@angular/http';
2+
import { Inject, Injectable } from '@angular/core';
3+
4+
import 'rxjs/Rx';
5+
import { Observable } from 'rxjs/Observable';
6+
import 'rxjs/add/observable/throw';
7+
import { Observer } from 'rxjs/Observer';
8+
import 'rxjs/add/operator/map';
9+
import 'rxjs/add/operator/catch';
10+
11+
import { AdalService } from './adal.service';
12+
import { BaseEndpoint } from './../app.constants';
13+
14+
@Injectable()
15+
export class BaseService<T> {
16+
headers: Headers;
17+
18+
constructor(private http: Http, @Inject(BaseEndpoint) private baseApiEndpoint, private adalService: AdalService) {
19+
20+
this.headers = new Headers({ 'Content-Type': 'application/json' });
21+
let jwt = this.adalService.accessToken;
22+
this.headers.append('Authorization', 'Bearer ' + jwt);
23+
}
24+
25+
getAll(): Observable<any> {
26+
return this.http.get(this.baseApiEndpoint, { headers: this.headers }).map(
27+
(res: Response) => {
28+
return res.json() as any[];
29+
}).catch(this.handleError);
30+
}
31+
32+
get(id: number): Observable<any> {
33+
return this.http.get(this.baseApiEndpoint + '/' + id).map((value, i) => {
34+
return <T>value.json()
35+
})
36+
.catch(this.handleError);
37+
}
38+
39+
private handleError(error: any) {
40+
console.error('server error:', error);
41+
if (error instanceof Response) {
42+
let errMessage = '';
43+
try {
44+
errMessage = error.json().error;
45+
} catch (err) {
46+
errMessage = error.statusText;
47+
}
48+
return Observable.throw(errMessage);
49+
}
50+
return Observable.throw(error || 'server error');
51+
}
52+
}

src/app/services/config.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { Injectable } from '@angular/core';
44
export class ConfigService {
55
constructor() {
66
}
7-
public get getAdalConfig(): any {
7+
public get AdalConfig(): any {
88
return {
9-
tenant: 'Your Tenant ID',
10-
clientId: 'Your Client ID',
9+
tenant: 'ENTER YOUR TENANT ID',
10+
clientId: 'ENTER YOUR CLIENT ID',
1111
redirectUri: window.location.origin + '/',
1212
postLogoutRedirectUri: window.location.origin + '/'
1313
};

src/app/services/value.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Http } from '@angular/http';
2+
import { BaseEndpoint } from './../app.constants';
3+
import { AdalService } from './adal.service';
4+
import { BaseService } from './base.service';
5+
import { Injectable, Inject } from '@angular/core';
6+
7+
@Injectable()
8+
export class ValueService extends BaseService<string>{
9+
constructor(http: Http, @Inject(BaseEndpoint) baseApiEndpoint, adalService: AdalService){
10+
super(http, baseApiEndpoint + '/api/values', adalService);
11+
}
12+
}

src/app/values/value.component.css

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h2>Values from ValueController</h2>
2+
3+
<ul>
4+
<li *ngFor="let value of values$ | async">{{value}}</li>
5+
</ul>

src/app/values/value.controller.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Component, OnInit, OnDestroy } from '@angular/core';
2+
import { Subject } from "rxjs/Subject";
3+
import { Observable } from 'rxjs/Observable';
4+
import 'rxjs/add/operator/takeUntil';
5+
import { ValueService } from './../services/value.service';
6+
7+
8+
@Component({
9+
templateUrl: './value.component.html',
10+
styleUrls: ['./value.component.css']
11+
})
12+
export class ValueComponent implements OnInit, OnDestroy {
13+
values$: Observable<any[]>;
14+
private ngUnsubscribe: Subject<void> = new Subject<void>();
15+
16+
17+
constructor(private valueService: ValueService) {
18+
}
19+
20+
ngOnInit() {
21+
this.values$ = this.valueService.getAll().takeUntil(this.ngUnsubscribe);
22+
}
23+
24+
ngOnDestroy() {
25+
this.ngUnsubscribe.complete();
26+
}
27+
}

0 commit comments

Comments
 (0)