Skip to content

Commit 6df7231

Browse files
author
Nathan Walker
committed
Update to Angular 2.0.0-alpha.31...Swapped out fetch with Http.
1 parent 8c15345 commit 6df7231

File tree

4 files changed

+50
-41
lines changed

4 files changed

+50
-41
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
},
2222
"homepage": "https://github.com/auth0/angular2-authentication-sample",
2323
"dependencies": {
24-
"angular2": "2.0.0-alpha.30",
24+
"angular2": "2.0.0-alpha.31",
2525
"raw-loader": "^0.5.1",
2626
"reflect-metadata": "^0.1.0",
27-
"rtts_assert": "2.0.0-alpha.29",
27+
"rtts_assert": "2.0.0-alpha.31",
2828
"rx": "^2.5.3",
2929
"zone.js": "^0.5.0",
3030
"bootstrap": "~3.3.4",

src/home/home.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import {Component, View} from 'angular2/angular2';
44
import {coreDirectives} from 'angular2/directives';
5-
import {status, text} from '../utils/fetch'
5+
import {status, text} from '../utils/fetch';
6+
import {Http} from 'angular2/http';
67
import { Router} from 'angular2/router';
78

89
let styles = require('./home.css');
@@ -23,7 +24,7 @@ export class Home {
2324
response: string;
2425
api: string;
2526

26-
constructor(public router: Router) {
27+
constructor(public router: Router, public http: Http) {
2728
this.jwt = localStorage.getItem('jwt');
2829
this.decodedJwt = this.jwt && window.jwt_decode(this.jwt);
2930
}
@@ -43,22 +44,24 @@ export class Home {
4344
_callApi(type, url) {
4445
this.response = null;
4546
this.api = type;
46-
window.fetch(url, {
47-
method: 'GET',
47+
this.http.get(url, {
4848
headers: {
4949
'Accept': 'application/json',
5050
'Content-Type': 'application/json',
5151
'Authorization': 'bearer ' + this.jwt
5252
}
5353
})
54-
.then(status)
55-
.then(text)
56-
.then((response) => {
57-
this.response = response;
58-
})
59-
.catch((error) => {
60-
this.response = error.message;
61-
});
54+
.toRx()
55+
.map(status)
56+
.map(text)
57+
.subscribe(
58+
response => {
59+
this.response = response;
60+
},
61+
error => {
62+
this.response = error.message;
63+
}
64+
)
6265
}
6366

6467
}

src/login/login.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// <reference path="../../typings/tsd.d.ts" />
22

33
import {Component, View} from 'angular2/angular2';
4-
import {status, json} from '../utils/fetch'
4+
import {status, json} from '../utils/fetch';
5+
import {Http} from 'angular2/http';
56
import { Router, RouterLink } from 'angular2/router';
67

78

@@ -18,13 +19,12 @@ let template = require('./login.html');
1819
directives: [RouterLink]
1920
})
2021
export class Login {
21-
constructor(public router: Router) {
22+
constructor(public router: Router, public http: Http) {
2223
}
2324

2425
login(event, username, password) {
2526
event.preventDefault();
26-
window.fetch('http://localhost:3001/sessions/create', {
27-
method: 'POST',
27+
this.http.post('http://localhost:3001/sessions/create', {
2828
headers: {
2929
'Accept': 'application/json',
3030
'Content-Type': 'application/json'
@@ -33,16 +33,19 @@ export class Login {
3333
username, password
3434
})
3535
})
36-
.then(status)
37-
.then(json)
38-
.then((response) => {
39-
localStorage.setItem('jwt', response.id_token);
40-
this.router.parent.navigate('/home');
41-
})
42-
.catch((error) => {
43-
alert(error.message);
44-
console.log(error.message);
45-
});
36+
.toRx()
37+
.map(status)
38+
.map(json)
39+
.subscribe(
40+
response => {
41+
localStorage.setItem('jwt', response.id_token);
42+
this.router.parent.navigate('/home');
43+
},
44+
error => {
45+
alert(error.message);
46+
console.log(error.message);
47+
}
48+
);
4649
}
4750

4851
signup(event) {

src/signup/signup.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {coreDirectives} from 'angular2/directives';
44
import {Component, View} from 'angular2/angular2';
55
import {status, json} from '../utils/fetch';
6+
import {Http} from 'angular2/http';
67
import { Router, RouterLink } from 'angular2/router';
78

89
let styles = require('./signup.css');
@@ -17,13 +18,12 @@ let template = require('./signup.html');
1718
template: template
1819
})
1920
export class Signup {
20-
constructor(public router: Router) {
21+
constructor(public router: Router, public http: Http) {
2122
}
2223

2324
signup(event, username, password) {
2425
event.preventDefault();
25-
window.fetch('http://localhost:3001/users', {
26-
method: 'POST',
26+
this.http.post('http://localhost:3001/users', {
2727
headers: {
2828
'Accept': 'application/json',
2929
'Content-Type': 'application/json'
@@ -32,16 +32,19 @@ export class Signup {
3232
username, password
3333
})
3434
})
35-
.then(status)
36-
.then(json)
37-
.then((response) => {
38-
localStorage.setItem('jwt', response.id_token);
39-
this.router.navigate('/home');
40-
})
41-
.catch((error) => {
42-
alert(error.message);
43-
console.log(error.message);
44-
});
35+
.toRx()
36+
.map(status)
37+
.map(json)
38+
.subscribe(
39+
response => {
40+
localStorage.setItem('jwt', response.id_token);
41+
this.router.navigate('/home');
42+
},
43+
error => {
44+
alert(error.message);
45+
console.log(error.message);
46+
}
47+
);
4548
}
4649

4750
login(event) {

0 commit comments

Comments
 (0)