Move login component to new v1 API

Fixes #65
This commit is contained in:
Will Abson
2016-05-16 11:24:23 +01:00
parent 22efd2e6ea
commit f7ddf59d1c
3 changed files with 39 additions and 43 deletions

View File

@@ -22,7 +22,7 @@ import { AlfrescoAuthenticationService } from '../../src/services/alfresco-authe
export class AuthenticationMock {
login(method: string, username: string, password: string) {
login(username: string, password: string) {
if (username === 'fake-username' && password === 'fake-password') {
return Observable.of(true);
} else {

View File

@@ -33,8 +33,6 @@ declare let __moduleName: string;
})
export class AlfrescoLoginComponent {
@Input()
method: string = 'POST';
@Output()
onSuccess = new EventEmitter();
@Output()
@@ -97,7 +95,7 @@ export class AlfrescoLoginComponent {
if (event) {
event.preventDefault();
}
this.auth.login(this.method, value.username, value.password)
this.auth.login(value.username, value.password)
.subscribe(
(token: any) => {
try {

View File

@@ -29,7 +29,7 @@ export class AlfrescoAuthenticationService {
token: string;
private _host: string = 'http://192.168.99.100:8080';
private _baseUrl: string = this._host + '/alfresco/service/api/';
private _baseUrl: string = this._host + '/alfresco/api/-default-/public/authentication/versions/1';
/**
* Constructor
@@ -48,40 +48,13 @@ export class AlfrescoAuthenticationService {
}
/**
* Method to delegate GET or POST login
* @param method
* Method to delegate to POST login
* @param username
* @param password
* @returns {Observable<R>|Observable<T>}
*/
login(method: string, username: string, password: string) {
if (method === 'GET') {
return this.loginGet(username, password);
} else if (method === 'POST') {
return this.loginPost(username, password);
} else {
return Observable.throw('Invalid method name the value should be GET or POST');
}
}
/**
* The method provide the login with GET Request
* @param username
* @param password
* @returns {Observable<R>|Observable<T>}
*/
loginGet(username: string, password: string) {
const searchParams = new URLSearchParams();
searchParams.set('u', username);
searchParams.set('pw', password);
return this.http.get(this._baseUrl + 'login', {search: searchParams})
.map((res: any) => {
let data = JSON.parse(xml2json(res.text(), ' '));
this.token = data.ticket;
this.saveJwt(this.token);
})
.catch(this.handleError);
login(username: string, password: string) {
return this.loginPost(username, password);
}
/**
@@ -91,22 +64,43 @@ export class AlfrescoAuthenticationService {
* @returns {Observable<R>|Observable<T>}
*/
loginPost(username: string, password: string) {
let credentials = '{ username: ' + username + ', password: ' + password + ' }';
let credentials = '{ "userId": "' + username + '", "password": "' + password + '" }';
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
return this.http.post(this._baseUrl + 'login', credentials, {
return this.http.post(this._baseUrl + '/tickets', credentials, {
headers: headers
})
.map((res: any) => {
let response = res.json();
this.token = response.data.ticket;
this.token = response.entry.id;
this.saveJwt(this.token);
})
.catch(this.handleError);
}
/**
* Delete the current login ticket from the server
*
* @returns {Observable<R>|Observable<T>}
*/
loginDelete() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Basic ' + btoa(this.token));
return this.http.delete(this._baseUrl + '/tickets/-me-', {
headers: headers
})
.map((res: any) => {
this.removeJwt();
this.token = undefined;
})
.catch(this.handleError);
}
/**
* The method save the toke in the localStorage
* @param jwt
@@ -117,15 +111,19 @@ export class AlfrescoAuthenticationService {
}
}
/**
* Remove the login token from localStorage
*/
removeJwt() {
localStorage.removeItem('token');
}
/**
* The method remove the token from the local storage
* @returns {Observable<T>}
*/
logout() {
this.token = undefined;
localStorage.removeItem('token');
return Observable.of(true);
return this.loginDelete();
}
/**
@@ -134,7 +132,7 @@ export class AlfrescoAuthenticationService {
* @returns {ErrorObservable}
*/
private handleError(error: Response) {
console.error(error.json().message);
console.error('Error when logging in', error);
return Observable.throw(error.json().message || 'Server error');
}
}