-
Notifications
You must be signed in to change notification settings - Fork 0
/
services
178 lines (155 loc) · 6.63 KB
/
services
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Injectable } from '@angular/core';
import { CanActivate,ActivatedRouteSnapshot,RouterStateSnapshot,Router } from '@angular/router';
import {Observable} from 'rxjs/Rx';
import {AuthService} from './authservice'
/**
* canActivate interface has canActivate() method
* aruments - ActivatedRouteSnapshot,RouterStateSnapshot
* return either Observable or a promise boolean
*/
@Injectable()
export class AuthManager implements CanActivate{
/**injecting any dependencies we need like authservice and router using contructor */
constructor(private authservice:AuthService,private router:Router){
console.log('inside AuthManager constructor');
}
canActivate(route:ActivatedRouteSnapshot,
state:RouterStateSnapshot){
console.log('canActivate --> '+localStorage.getItem('currentUser'));
if (localStorage.getItem('currentUser')) {
// logged in so return true
return true;
}
// not logged in so redirect to login page
this.router.navigate(['/login']);
return false;
}
}
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import {User} from '../login/user';
@Injectable()
export class AuthService {
public token: string;
isAuthenticated: boolean = false;
constructor(private http: Http) {
console.log('constructor auth service initaited');
// set token if saved in local storage
var currentUser = JSON.parse(localStorage.getItem('currentUser'));
this.token = currentUser && currentUser.token;
console.log('inside authservice constructor current user token '+this.token);
}
authenticateNow(username: string, password: string) {
console.log('authenticateNow'+username);
return this.http.post('/api/authenticate', JSON.stringify({ username: username, password: password }))
.map((response: Response) => {
// login successful if there's a jwt token in the response
let token = response.json() && response.json().token;
if (token) {
// set token property
this.token = token;
// store username and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify({ username: username, token: token }));
// return true to indicate successful login
return true;
} else {
// return false to indicate failed login
return false;
}
});
}
logout(): void {
// clear token remove user from local storage to log user out
console.log('logged out');
this.token = null;
localStorage.removeItem('currentUser');
}
}
import {Injectable} from '@angular/core';
/***
* this is a provider we will use through out the application to inject any routing parameter;
* hence having public defination; include this in app.module
*
*/
@Injectable()
export class RoutingDataValue{
public routingDataStorage:any;
public constructor(){
console.log('routing data injected : --> '+this.routingDataStorage);
}
}
import {Injectable} from '@angular/core';
import { Http, Headers,Response,RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/toPromise';
import {SearchResult} from '../search/search.result';
import {InqSearchComponent} from '../search/inqsearch.component'
import {AuthService} from '../services/authservice';
/** when we will use http we have to import httpmodule provider in out module ts to tell angular to inject
* http service wen required
*/
@Injectable()
export class SearchService{
private searchUrl:string = 'http://jsonplaceholder.typicode.com/posts/1/comments';
constructor(private _http:Http , private authenticationService: AuthService){
}
getINQSearchResult(values:string):Observable<SearchResult[]>{
console.log('method getINQSearchResult values --> ' +values);
console.log('method getINQSearchResult values this.authenticationService.token --> ' +this.authenticationService.token );
let headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
let options = new RequestOptions({ headers: headers });
// // get users from api
// return this.http.get('/api/users', options)
// .map((response: Response) => response.json());
return this._http.get(this.searchUrl,options)
.map(this.extractData)
.do(value => console.log("data received : "+value))
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || [];
}
private handleError (error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
let errMsg: string;
console.log('method getINQSearchResult handleError +' +error.stringify);
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
}
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'
import { AuthService } from '../services/authservice';
import { User } from '../login/user';
@Injectable()
export class UserService {
constructor(
private http: Http,
private authenticationService: AuthService) {
console.log('inside user service');
}
getUsers(): Observable<User[]> {
// add authorization header with jwt token
console.log ('inside get users user service ts');
let headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
let options = new RequestOptions({ headers: headers });
// get users from api
return this.http.get('/api/users', options)
.map((response: Response) => response.json());
}
}