Skip to content

Commit ad92036

Browse files
committed
refactor(client): use HttpClient, new angular-jwt
1 parent e4851c0 commit ad92036

File tree

5 files changed

+26
-81
lines changed

5 files changed

+26
-81
lines changed

Diff for: templates/app/_package.json

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"@angular/compiler": "^5.0.1",
5959
"@angular/core": "^5.0.1",
6060
"@angular/forms": "^5.0.1",
61-
"@angular/http": "^5.0.1",
6261
"@angular/platform-browser": "^5.0.1",
6362
"@angular/platform-browser-dynamic": "^5.0.1",
6463
"@angular/router": "^5.0.1",

Diff for: templates/app/client/app/app.module.js

+10-36
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ import {
55
Provider,<% } %>
66
} from '@angular/core';
77
import { BrowserModule } from '@angular/platform-browser';
8-
import {
9-
Http,
10-
HttpModule,
11-
BaseRequestOptions,<% if(filters.ts || filters.flow) { %>
12-
RequestOptions,
13-
RequestOptionsArgs,<% } %>
14-
} from '@angular/http';
158
import { HttpClientModule } from '@angular/common/http';
169
import {
1710
removeNgStyles,
@@ -22,40 +15,18 @@ import {
2215
import { UIRouterModule } from 'ui-router-ng2';<% } %>
2316
<%_ if (filters.ngroute) { -%>
2417
import { RouterModule, Routes } from '@angular/router';<% } %>
25-
import { AuthHttp, AuthConfig } from 'angular2-jwt';
2618

2719
import { AppComponent } from './app.component';
2820
import { MainModule } from './main/main.module';
2921
import { DirectivesModule } from '../components/directives.module';<% if(filters.auth) { %>
22+
import { JwtModule } from '@auth0/angular-jwt';
3023
import { AccountModule } from './account/account.module';
3124
import { AdminModule } from './admin/admin.module';<% } %>
3225

3326
import constants from './app.constants';
3427

35-
export function getAuthHttp(http) {
36-
return new AuthHttp(new AuthConfig({
37-
noJwtError: true,
38-
globalHeaders: [{'Accept': 'application/json'}],
39-
tokenGetter: (() => localStorage.getItem('id_token')),
40-
}), http);
41-
}
42-
43-
let providers: Provider[] = [{
44-
provide: AuthHttp,
45-
useFactory: getAuthHttp,
46-
deps: [Http]
47-
}];
48-
49-
if(constants.env === 'development') {
50-
@Injectable()
51-
class HttpOptions extends BaseRequestOptions {
52-
merge(options: RequestOptionsArgs): RequestOptions {
53-
options.url = `http://localhost:9000${options.url}`;
54-
return super.merge(options);
55-
}
56-
}
57-
58-
providers.push({ provide: RequestOptions, useClass: HttpOptions });
28+
export function tokenGetter() {
29+
return localStorage.getItem('id_token');
5930
}
6031

6132
const appRoutes: Routes = [{ path: '',
@@ -64,11 +35,14 @@ const appRoutes: Routes = [{ path: '',
6435
}];
6536

6637
@NgModule({
67-
providers,
6838
imports: [
6939
BrowserModule,
70-
HttpModule,
71-
HttpClientModule,
40+
HttpClientModule,<% if(filters.auth) { %>
41+
JwtModule.forRoot({
42+
config: {
43+
tokenGetter,
44+
}
45+
}),<% } %>
7246
<%_ if (filters.uirouter) { -%>
7347
UIRouterModule.forRoot(),<% } %>
7448
<%_ if (filters.ngroute) { -%>
@@ -81,7 +55,7 @@ const appRoutes: Routes = [{ path: '',
8155
declarations: [
8256
AppComponent,
8357
],
84-
bootstrap: [AppComponent]
58+
bootstrap: [AppComponent],
8559
})
8660
export class AppModule {
8761
static parameters = [ApplicationRef];

Diff for: templates/app/client/components/auth(auth)/auth.service.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Injectable, EventEmitter, Output } from '@angular/core';
2-
import { AuthHttp } from 'angular2-jwt';
32
import { UserService } from './user.service';
4-
import { Http } from '@angular/http';
3+
import { HttpClient } from '@angular/common/http';
54
import 'rxjs/add/operator/toPromise';
6-
import { safeCb, extractData } from '../util';
5+
import { safeCb } from '../util';
76
import constants from '../../app/app.constants';
87

98
// @flow
@@ -12,22 +11,18 @@ class User {
1211
name = '';
1312
email = '';
1413
role = '';
15-
$promise = undefined;
1614
}
1715

1816
@Injectable()
1917
export class AuthService {
2018
_currentUser: User = new User();
2119
@Output() currentUserChanged = new EventEmitter(true);
2220
userRoles = constants.userRoles || [];
23-
Http;
24-
AuthHttp;
2521
UserService;
2622

27-
static parameters = [Http, AuthHttp, UserService];
28-
constructor(<%= private() %>http: Http, <%= private() %>authHttp: AuthHttp, <%= private() %>userService: UserService) {
29-
this.Http = http;
30-
this.AuthHttp = authHttp;
23+
static parameters = [HttpClient, UserService];
24+
constructor(<%= private() %>http: HttpClient, <%= private() %>userService: UserService) {
25+
this.http = http;
3126
this.UserService = userService;
3227

3328
if(localStorage.getItem('id_token')) {
@@ -69,12 +64,11 @@ export class AuthService {
6964
* @return {Promise}
7065
*/
7166
login({email, password}, callback) {
72-
return this.Http.post('/auth/local', {
67+
return this.http.post('/auth/local', {
7368
email,
7469
password
7570
})
7671
.toPromise()
77-
.then(extractData)
7872
.then(res => {
7973
localStorage.setItem('id_token', res.token);
8074
return this.UserService.get().toPromise();
+10-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow
22
import { Injectable } from '@angular/core';
3-
import { Response } from '@angular/http';
4-
import { AuthHttp } from 'angular2-jwt';
3+
import { HttpClient } from '@angular/common/http';
54
import { Observable } from 'rxjs/Rx';
65
import 'rxjs/add/operator/map';
76
import 'rxjs/add/operator/catch';
@@ -15,42 +14,27 @@ type UserType = {
1514
email?: string;
1615
};
1716

18-
function handleError(err) {
19-
return Observable.throw(err.json() || 'Server error');
20-
}
21-
2217
@Injectable()
2318
export class UserService {
24-
AuthHttp;
25-
26-
static parameters = [AuthHttp];
27-
constructor(<%= private() %>authHttp: AuthHttp) {
28-
this.AuthHttp = authHttp;
19+
static parameters = [HttpClient];
20+
constructor(<%= private() %>http: HttpClient) {
21+
this.http = http;
2922
}
3023

3124
query(): Observable<UserType[]> {
32-
return this.AuthHttp.get('/api/users/')
33-
.map((res: Response) => res.json())
34-
.catch(handleError);
25+
return this.http.get('/api/users/');
3526
}
3627
get(user<% if(filters.ts) { %>: UserType<% } %> = {id: 'me'}): Observable<UserType> {
37-
return this.AuthHttp.get(`/api/users/${user.id || user._id}`)
38-
.map((res: Response) => res.json())
39-
.catch(handleError);
28+
return this.http.get(`/api/users/${user.id || user._id}`);
4029
}
4130
create(user: UserType) {
42-
return this.AuthHttp.post('/api/users/', user)
43-
.map((res: Response) => res.json())
44-
.catch(handleError);
31+
return this.http.post('/api/users/', user);
4532
}
4633
changePassword(user, oldPassword, newPassword) {
47-
return this.AuthHttp.put(`/api/users/${user.id || user._id}/password`, {oldPassword, newPassword})
48-
.map((res: Response) => res.json())
49-
.catch(handleError);
34+
return this.http.put(`/api/users/${user.id || user._id}/password`, {oldPassword, newPassword});
5035
}
5136
remove(user) {
52-
return this.AuthHttp.delete(`/api/users/${user.id || user._id}`)
53-
.map(() => user)
54-
.catch(handleError);
37+
return this.http.delete(`/api/users/${user.id || user._id}`)
38+
.map(() => user);
5539
}
5640
}

Diff for: templates/app/client/components/util.js

-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
isFunction,
77
noop,
88
} from 'lodash';
9-
import { Response } from '@angular/http';
109

1110
/**
1211
* Return a callback or noop function
@@ -60,8 +59,3 @@ export function isSameOrigin(url, origins) {
6059
});
6160
return origins.length >= 1;
6261
}
63-
64-
export function extractData(res: Response) {
65-
if(!res.text()) return {};
66-
return res.json() || { };
67-
}

0 commit comments

Comments
 (0)