fix token in the core

This commit is contained in:
Mario Romano 2016-06-02 15:15:51 +01:00
parent 6a0b8c3e3f
commit 574b62063d

View File

@ -25,7 +25,6 @@ import { AlfrescoSettingsService } from './AlfrescoSettingsService.service';
*/ */
@Injectable() @Injectable()
export class AlfrescoAuthenticationService { export class AlfrescoAuthenticationService {
token: string;
private _authUrl: string = '/alfresco/api/-default-/public/authentication/versions/1'; private _authUrl: string = '/alfresco/api/-default-/public/authentication/versions/1';
@ -34,7 +33,6 @@ export class AlfrescoAuthenticationService {
* @param http * @param http
*/ */
constructor(public http: Http, private alfrescoSettingsService: AlfrescoSettingsService) { constructor(public http: Http, private alfrescoSettingsService: AlfrescoSettingsService) {
this.token = localStorage.getItem('token');
} }
getBaseUrl(): string { getBaseUrl(): string {
@ -77,9 +75,8 @@ export class AlfrescoAuthenticationService {
}) })
.map((res: any) => { .map((res: any) => {
let response = res.json(); let response = res.json();
this.token = response.entry.id; this.saveToken(response.entry.id);
this.saveJwt(this.token); return this.getToken();
return this.token;
}) })
.catch(this.handleError); .catch(this.handleError);
} }
@ -92,32 +89,40 @@ export class AlfrescoAuthenticationService {
loginDelete() { loginDelete() {
let headers = new Headers(); let headers = new Headers();
headers.append('Content-Type', 'application/json'); headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Basic ' + btoa(this.token)); headers.append('Authorization', 'Basic ' + btoa(this.getToken()));
return this.http.delete(this.getBaseUrl() + '/tickets/-me-', { return this.http.delete(this.getBaseUrl() + '/tickets/-me-', {
headers: headers headers: headers
}) })
.map((res: any) => { .map((res: any) => {
this.removeJwt(); this.removeToken();
this.token = undefined; this.saveToken('');
}) })
.catch(this.handleError); .catch(this.handleError);
} }
/** /**
* The method save the toke in the localStorage * Return the token stored in the localStorage
* @param jwt * @param token
*/ */
saveJwt(jwt) { public getToken (): string {
if (jwt) { return localStorage.getItem('token');
localStorage.setItem('token', jwt); }
/**
* The method save the toke in the localStorage
* @param token
*/
public saveToken(token): void {
if (token) {
localStorage.setItem('token', token);
} }
} }
/** /**
* Remove the login token from localStorage * Remove the login token from localStorage
*/ */
removeJwt() { public removeToken() :void {
localStorage.removeItem('token'); localStorage.removeItem('token');
} }
@ -125,7 +130,7 @@ export class AlfrescoAuthenticationService {
* The method remove the token from the local storage * The method remove the token from the local storage
* @returns {Observable<T>} * @returns {Observable<T>}
*/ */
logout() { public logout() {
return this.loginDelete(); return this.loginDelete();
} }