src/lib/core/http/auth.service.ts
constructor(backend: XHRBackend, defaultOptions: AuthorizeRequestOptions, config: ConfigService)
|
Defined in src/lib/core/http/auth.service.ts:22
|
Public login |
login(username: string, password: string)
|
Defined in src/lib/core/http/auth.service.ts:36
|
Returns :
Observable<>
|
Public logout |
logout()
|
Defined in src/lib/core/http/auth.service.ts:61
|
Returns :
void
|
Protected checkLogin |
checkLogin(body: any)
|
Defined in src/lib/core/http/auth.service.ts:65
|
Returns :
void
|
Private reload |
reload()
|
Defined in src/lib/core/http/auth.service.ts:84
|
Returns :
void
|
Private secretkey |
secretkey()
|
Defined in src/lib/core/http/auth.service.ts:88
|
Returns :
void
|
Protected apiUrl |
apiUrl: |
Defined in src/lib/core/http/auth.service.ts:22
|
Private isInLoginProcess |
isInLoginProcess: |
Default value : false
|
Defined in src/lib/core/http/auth.service.ts:18
|
Private location |
location: |
Type : any
|
Defined in src/lib/core/http/auth.service.ts:20
|
Private storage |
storage: |
Type : any
|
Defined in src/lib/core/http/auth.service.ts:19
|
Private windowRef |
windowRef: |
Defined in src/lib/core/http/auth.service.ts:21
|
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {
Http,
Response,
Headers,
XHRBackend, RequestOptions
} from '@angular/http';
import {AuthorizeRequestOptions} from './authorize-request-options';
import {WindowRefService} from '../../shared/services/window-ref.service';
import {ConfigService} from '../config/config.service';
@Injectable()
export class AuthService extends Http {
private static isInLoginProcess = false;
private storage: any;
private location: any;
private windowRef = new WindowRefService();
protected apiUrl = '';
constructor(
backend: XHRBackend,
defaultOptions: AuthorizeRequestOptions,
config: ConfigService
) {
super(backend, defaultOptions);
this.apiUrl = config.getConfig('apiUrl');
this.storage = this.windowRef.nativeObject('localStorage');
this.location = this.windowRef.nativeObject('location');
}
public login(username?: string, password?: string): Observable<Response> {
this.storage.removeItem('currentToken');
if (username && password) {
return this.put(
'auth',
JSON.stringify({
username: username,
password: password
}),
this.secretkey()
).map((response: Response) => {
return response.json();
});
}
return this.put(
'auth',
'',
this.secretkey()
).map((response: Response) => {
return response.json();
});
}
public logout() {
this.storage.removeItem('currentToken');
}
protected checkLogin(body: any) {
if (body.error && body.login === false) {
if (!AuthService.isInLoginProcess) {
AuthService.isInLoginProcess = true;
this.login().subscribe((infoToken) => {
AuthService.isInLoginProcess = false;
if (infoToken['data'].accessToken) {
this.storage.setItem('currentToken', infoToken['data'].accessToken);
this.reload();
} else {
throw new Error('need to login');
}
});
}
}
}
private reload() {
this.location.reload();
}
private secretkey() {
const key = '5d98e9644cc8e3b1aea67e0c23c76b4c7ee3fb91546402fad725a15e';
if (key) {
return new RequestOptions({
headers: new Headers({
'Authorization': 'Key ' + key
})
});
}
}
}