mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
@@ -22,7 +22,7 @@ import { AlfrescoAuthenticationService } from '../../src/services/alfresco-authe
|
|||||||
|
|
||||||
export class AuthenticationMock {
|
export class AuthenticationMock {
|
||||||
|
|
||||||
login(method: string, username: string, password: string) {
|
login(username: string, password: string) {
|
||||||
if (username === 'fake-username' && password === 'fake-password') {
|
if (username === 'fake-username' && password === 'fake-password') {
|
||||||
return Observable.of(true);
|
return Observable.of(true);
|
||||||
} else {
|
} else {
|
||||||
|
@@ -33,8 +33,6 @@ declare let __moduleName: string;
|
|||||||
|
|
||||||
})
|
})
|
||||||
export class AlfrescoLoginComponent {
|
export class AlfrescoLoginComponent {
|
||||||
@Input()
|
|
||||||
method: string = 'POST';
|
|
||||||
@Output()
|
@Output()
|
||||||
onSuccess = new EventEmitter();
|
onSuccess = new EventEmitter();
|
||||||
@Output()
|
@Output()
|
||||||
@@ -97,7 +95,7 @@ export class AlfrescoLoginComponent {
|
|||||||
if (event) {
|
if (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
this.auth.login(this.method, value.username, value.password)
|
this.auth.login(value.username, value.password)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
(token: any) => {
|
(token: any) => {
|
||||||
try {
|
try {
|
||||||
|
@@ -29,7 +29,7 @@ export class AlfrescoAuthenticationService {
|
|||||||
token: string;
|
token: string;
|
||||||
|
|
||||||
private _host: string = 'http://192.168.99.100:8080';
|
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
|
* Constructor
|
||||||
@@ -48,40 +48,13 @@ export class AlfrescoAuthenticationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to delegate GET or POST login
|
* Method to delegate to POST login
|
||||||
* @param method
|
|
||||||
* @param username
|
* @param username
|
||||||
* @param password
|
* @param password
|
||||||
* @returns {Observable<R>|Observable<T>}
|
* @returns {Observable<R>|Observable<T>}
|
||||||
*/
|
*/
|
||||||
login(method: string, username: string, password: string) {
|
login(username: string, password: string) {
|
||||||
if (method === 'GET') {
|
return this.loginPost(username, password);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,22 +64,43 @@ export class AlfrescoAuthenticationService {
|
|||||||
* @returns {Observable<R>|Observable<T>}
|
* @returns {Observable<R>|Observable<T>}
|
||||||
*/
|
*/
|
||||||
loginPost(username: string, password: string) {
|
loginPost(username: string, password: string) {
|
||||||
let credentials = '{ username: ' + username + ', password: ' + password + ' }';
|
let credentials = '{ "userId": "' + username + '", "password": "' + password + '" }';
|
||||||
|
|
||||||
let headers = new Headers();
|
let headers = new Headers();
|
||||||
headers.append('Content-Type', 'application/json');
|
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
|
headers: headers
|
||||||
})
|
})
|
||||||
.map((res: any) => {
|
.map((res: any) => {
|
||||||
let response = res.json();
|
let response = res.json();
|
||||||
this.token = response.data.ticket;
|
this.token = response.entry.id;
|
||||||
this.saveJwt(this.token);
|
this.saveJwt(this.token);
|
||||||
})
|
})
|
||||||
.catch(this.handleError);
|
.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
|
* The method save the toke in the localStorage
|
||||||
* @param jwt
|
* @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
|
* The method remove the token from the local storage
|
||||||
* @returns {Observable<T>}
|
* @returns {Observable<T>}
|
||||||
*/
|
*/
|
||||||
logout() {
|
logout() {
|
||||||
this.token = undefined;
|
return this.loginDelete();
|
||||||
localStorage.removeItem('token');
|
|
||||||
|
|
||||||
return Observable.of(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,7 +132,7 @@ export class AlfrescoAuthenticationService {
|
|||||||
* @returns {ErrorObservable}
|
* @returns {ErrorObservable}
|
||||||
*/
|
*/
|
||||||
private handleError(error: Response) {
|
private handleError(error: Response) {
|
||||||
console.error(error.json().message);
|
console.error('Error when logging in', error);
|
||||||
return Observable.throw(error.json().message || 'Server error');
|
return Observable.throw(error.json().message || 'Server error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user