|
@@ -17,7 +17,9 @@ export class TokenService {
|
|
|
constructor(public http: Http) {
|
|
|
}
|
|
|
|
|
|
- // Get token
|
|
|
+ /**
|
|
|
+ * Pass token auth
|
|
|
+ */
|
|
|
getToken(): void {
|
|
|
// let response = this.getTokenFromServer();
|
|
|
//
|
|
@@ -29,12 +31,16 @@ export class TokenService {
|
|
|
this.checkTokenOnServer();
|
|
|
}
|
|
|
|
|
|
- // Check if token is present locally
|
|
|
+ /**
|
|
|
+ * Check if token is present locally
|
|
|
+ */
|
|
|
tokenIsPresent(): boolean {
|
|
|
return this.getTokenFromLocal() ? true : false;
|
|
|
}
|
|
|
|
|
|
- // Check local token on the server
|
|
|
+ /**
|
|
|
+ * Check local token on the server
|
|
|
+ */
|
|
|
checkTokenOnServer(): Observable<Response> {
|
|
|
let body = 'token=' + this.getTokenFromLocal();
|
|
|
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
|
|
@@ -42,38 +48,53 @@ export class TokenService {
|
|
|
|
|
|
return this.http.post(this.normalizeCheckUrl(this.url), body, options).map(this.extractCheckData).catch(this.handleError);
|
|
|
}
|
|
|
+ /**
|
|
|
+ * Extract check value from token verification
|
|
|
+ */
|
|
|
|
|
|
- // Extract check value from token verification
|
|
|
private extractCheckData(response: Response): boolean {
|
|
|
let body = response.json();
|
|
|
return body.token == 'valid' ? true : false;
|
|
|
}
|
|
|
|
|
|
- // Get token from
|
|
|
+ /**
|
|
|
+ * Get token from local storage
|
|
|
+ */
|
|
|
private getTokenFromLocal(): string {
|
|
|
return localStorage.getItem('odoo');
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Store token locally
|
|
|
+ */
|
|
|
private setLocalToken(value): void {
|
|
|
localStorage.setItem('odoo', value);
|
|
|
}
|
|
|
|
|
|
- // Check if introduced url is valid
|
|
|
+ /**
|
|
|
+ * Check if introduced url is valid
|
|
|
+ */
|
|
|
private checkUrl(): boolean {
|
|
|
return this.url.includes('.');
|
|
|
}
|
|
|
|
|
|
- // Normalize provide url
|
|
|
+ /**
|
|
|
+ * Handle error if ocurred
|
|
|
+ */
|
|
|
private normalizeCheckUrl(url: string): string {
|
|
|
return (url.startsWith('http://') ? url : 'http://' + url) + '/api/check';
|
|
|
}
|
|
|
|
|
|
- // Normalize provide url
|
|
|
+ /**
|
|
|
+ * Normalize provide url
|
|
|
+ */
|
|
|
private normalizeJwtUrl(url: string): string {
|
|
|
return (url.startsWith('http://') ? url : 'http://' + url) + '/api/jwt';
|
|
|
}
|
|
|
|
|
|
- // Get token from server
|
|
|
+ /**
|
|
|
+ * Get token from server
|
|
|
+ */
|
|
|
private getTokenFromServer(): Observable<Token> {
|
|
|
let body = 'username=' + this.username + '&password=' + this.password;
|
|
|
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
|
|
@@ -82,7 +103,9 @@ export class TokenService {
|
|
|
return this.http.post(this.normalizeJwtUrl(this.url), body, options).map(this.extractTokenData).catch(this.handleError);
|
|
|
}
|
|
|
|
|
|
- // Extract token data from http response
|
|
|
+ /**
|
|
|
+ * Extract token data from http response
|
|
|
+ */
|
|
|
private extractTokenData(response: Response) {
|
|
|
let body = response.json();
|
|
|
let token = new Token(body.token);
|
|
@@ -90,7 +113,9 @@ export class TokenService {
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
- // Handle error if ocurred
|
|
|
+ /**
|
|
|
+ * Handle error if ocurred
|
|
|
+ */
|
|
|
private handleError(error: any) {
|
|
|
let msg = error._body;
|
|
|
|