#9 Automatic login for documentlist

This commit is contained in:
Denys Vuika
2016-05-26 12:12:04 +01:00
parent 4d53cd107e
commit 57f3e8bef2
3 changed files with 47 additions and 8 deletions

View File

@@ -25,10 +25,6 @@
<!-- Additional Alfresco libraries --> <!-- Additional Alfresco libraries -->
<script src="node_modules/alfresco-core-rest-api/bundle.js"></script> <script src="node_modules/alfresco-core-rest-api/bundle.js"></script>
<script>
localStorage.setItem('token', '<AUTH-TOKEN>');
</script>
<script> <script>
System.config({ System.config({
defaultJSExtensions: true, defaultJSExtensions: true,

View File

@@ -15,9 +15,10 @@
* limitations under the License. * limitations under the License.
*/ */
import { Component } from 'angular2/core'; import { Component, OnInit } from 'angular2/core';
import { bootstrap } from 'angular2/platform/browser'; import { bootstrap } from 'angular2/platform/browser';
import { HTTP_PROVIDERS } from 'angular2/http'; import { Observable } from 'rxjs/Rx';
import { HTTP_PROVIDERS, Http, Headers, Response } from 'angular2/http';
import { import {
ALFRESCO_CORE_PROVIDERS, ALFRESCO_CORE_PROVIDERS,
@@ -36,7 +37,7 @@ import { AlfrescoPipeTranslate, AlfrescoTranslationService } from 'ng2-alfresco-
selector: 'alfresco-documentlist-demo', selector: 'alfresco-documentlist-demo',
template: ` template: `
<div class="container"> <div class="container">
<alfresco-document-list> <alfresco-document-list *ngIf="authenticated">
<content-columns> <content-columns>
<content-column source="$thumbnail"></content-column> <content-column source="$thumbnail"></content-column>
<content-column <content-column
@@ -114,8 +115,12 @@ import { AlfrescoPipeTranslate, AlfrescoTranslationService } from 'ng2-alfresco-
providers: [DOCUMENT_LIST_PROVIDERS], providers: [DOCUMENT_LIST_PROVIDERS],
pipes: [AlfrescoPipeTranslate] pipes: [AlfrescoPipeTranslate]
}) })
class DocumentListDemo { class DocumentListDemo implements OnInit {
authenticated: boolean;
constructor( constructor(
private http: Http,
settings: AlfrescoSettingsService, settings: AlfrescoSettingsService,
translation: AlfrescoTranslationService, translation: AlfrescoTranslationService,
documentActions: DocumentActionsService) { documentActions: DocumentActionsService) {
@@ -125,6 +130,10 @@ class DocumentListDemo {
documentActions.setHandler('my-handler', this.myDocumentActionHandler.bind(this)); documentActions.setHandler('my-handler', this.myDocumentActionHandler.bind(this));
} }
ngOnInit() {
this.login();
}
myDocumentActionHandler(obj: any) { myDocumentActionHandler(obj: any) {
window.alert('my custom action handler'); window.alert('my custom action handler');
} }
@@ -136,6 +145,29 @@ class DocumentListDemo {
myFolderAction1(event) { myFolderAction1(event) {
alert('Custom folder action for ' + event.value.displayName); alert('Custom folder action for ' + event.value.displayName);
} }
private handleError(error: Response) {
console.error('Error when logging in', error);
return Observable.throw(error.json().message || 'Server error');
}
login() {
let host = 'http://192.168.99.100:8080';
let credentials = { "userId": "admin", "password": "admin" };
let url = `${host}/alfresco/api/-default-/public/authentication/versions/1/tickets`;
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
this.http.post(url, JSON.stringify(credentials), { headers: headers })
.map(res => res.json().entry.id)
.catch(this.handleError)
.subscribe(token => {
localStorage.setItem('token', token);
this.authenticated = true;
});
}
} }
bootstrap(DocumentListDemo, [ bootstrap(DocumentListDemo, [

View File

@@ -6,6 +6,17 @@ Install:
npm install npm install
``` ```
Update host and credentials
**src/main.ts**
```ts
login() {
let host = 'http://192.168.99.100:8080';
let credentials = { "userId": "admin", "password": "admin" };
...
}
```
Run the project: Run the project:
``` ```