mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
@@ -42,8 +42,8 @@ export class Login {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onSubmit(value: any, event) {
|
onSubmit(value: any, event) {
|
||||||
//event.preventDefault();
|
event.preventDefault();
|
||||||
this.auth.login(value.username, value.password)
|
this.auth.login('POST', value.username, value.password)
|
||||||
.subscribe(
|
.subscribe(
|
||||||
//(token: any) => this.router.navigate(['../Home']),
|
//(token: any) => this.router.navigate(['../Home']),
|
||||||
(token: any) => this.router.navigate(['Home']),
|
(token: any) => this.router.navigate(['Home']),
|
||||||
|
@@ -1,11 +1,15 @@
|
|||||||
import {Injectable} from "angular2/core";
|
import {Injectable} from "angular2/core";
|
||||||
import {Observable} from 'rxjs/Rx';
|
import {Observable} from 'rxjs/Rx';
|
||||||
|
import {Http, Headers, URLSearchParams} from 'angular2/http';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class Authentication {
|
export class Authentication {
|
||||||
token: string;
|
token: string;
|
||||||
|
|
||||||
constructor() {
|
private _host: string = 'http://192.168.99.100:8080';
|
||||||
|
private _baseUrl: string = this._host + '/alfresco/service/api/';
|
||||||
|
|
||||||
|
constructor(public http: Http) {
|
||||||
this.token = localStorage.getItem('token');
|
this.token = localStorage.getItem('token');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -13,14 +17,49 @@ export class Authentication {
|
|||||||
return !!localStorage.getItem('token');
|
return !!localStorage.getItem('token');
|
||||||
}
|
}
|
||||||
|
|
||||||
login(username: String, password: String) {
|
login(method:String, username:String, password:String) {
|
||||||
if (username === 'test' && password === 'test') {
|
if (method === 'GET') {
|
||||||
this.token = 'token';
|
return this.loginGet(username, password);
|
||||||
localStorage.setItem('token', this.token);
|
} else {
|
||||||
return Observable.of('token');
|
return this.loginPost(username, password);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Observable.throw('authentication failure');
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
loginPost(username:String, password:String) {
|
||||||
|
var credentials = "{ username: " + username+ ", password: "+ password +" }";
|
||||||
|
|
||||||
|
var headers = new Headers();
|
||||||
|
headers.append('Content-Type', 'application/json');
|
||||||
|
|
||||||
|
return this.http.post(this._baseUrl+ 'login', credentials, {
|
||||||
|
headers: headers
|
||||||
|
})
|
||||||
|
.map((res:any) => {
|
||||||
|
let response = res.json();
|
||||||
|
this.token = response.data.ticket;
|
||||||
|
this.saveJwt(this.token);
|
||||||
|
})
|
||||||
|
.catch(this.handleError);
|
||||||
|
}
|
||||||
|
|
||||||
|
saveJwt(jwt) {
|
||||||
|
if(jwt) {
|
||||||
|
localStorage.setItem('token', jwt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
@@ -29,4 +68,11 @@ export class Authentication {
|
|||||||
|
|
||||||
return Observable.of(true);
|
return Observable.of(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private handleError (error: Response) {
|
||||||
|
// in a real world app, we may send the error to some remote logging infrastructure
|
||||||
|
// instead of just logging it to the console
|
||||||
|
console.error(error);
|
||||||
|
return Observable.throw(error.json().error || 'Server error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -26,6 +26,8 @@
|
|||||||
<script src="node_modules/jquery/dist/jquery.min.js"></script>
|
<script src="node_modules/jquery/dist/jquery.min.js"></script>
|
||||||
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
|
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
|
||||||
|
|
||||||
|
<script src="./script/xml2json.js"></script>
|
||||||
|
|
||||||
<!-- 2. Configure SystemJS -->
|
<!-- 2. Configure SystemJS -->
|
||||||
<script>
|
<script>
|
||||||
System.config({
|
System.config({
|
||||||
|
Reference in New Issue
Block a user