From b117bd7d8fa45066caf0d7a7b88deb5014ba284c Mon Sep 17 00:00:00 2001 From: Eric BREHAULT Date: Sun, 20 Oct 2019 17:35:15 +0200 Subject: [PATCH] add composite service --- .travis.yml | 3 -- CHANGELOG.md | 5 +++ projects/grange-core/package.json | 2 +- projects/grange-core/src/index.ts | 1 + .../src/lib/authentication.service.ts | 36 +++++++++++-------- projects/grange-core/src/lib/core.service.ts | 20 +++++++++++ projects/grange-core/src/lib/interfaces.ts | 12 ++++++- .../grange-core/src/lib/resource.service.ts | 3 +- 8 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 projects/grange-core/src/lib/core.service.ts diff --git a/.travis.yml b/.travis.yml index db43327..0a7bb4f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,3 @@ deploy: on: repo: guillotinaweb/grange-core branch: master -env: - global: - secure: XA3j4Q1ZTgmteIfnNRdRvZI4iuNmDgcvmbYzdNcKPVIslFVskZ5dP/t5t+owjR4ARTcEWqcnyryKrKy+J0UUhUMcEvMRSqZnvfKwU3gN0jr0C7tVfLai5NplOlrnyNxqGK6G/XG3riPdzxCnEPkp3WPcimtOoK+QgSucBMZCtN2uIBMqD5KCg+27xeJvGx8U7JYa0sL2GnEvR6Pqsbket71El5vrdx6cGglIPKKwiWTzFDhHkSeTBiQCcfHvkzYxIh9Ur2ObDYAL+oAFNijetmc3dOXNG0eWuZOrrxsqKA23x7ef8InfSoCCwRQOfeTq103sJdv/BI1Yc2624uQDV7INX8u9troNAI8V2Ocm2G4gkN0PHo2zQayIZBXJdWGDpET490NvVPl+DqaU3dDSSzP6V2ALTEngYZR1y1U1iYvsCWD5CoFERQ7Iw3FyKW5rdyFx+JEVP3bLqvvSdO6vW35irAHw7OXwqKotpNIct7B05H4BwPG3qIWZdn201CllYHJ+k/dqTWYRA66Gj/LUIGtd1oAiE+2R+warCiDZ6zAOnPirchcf9GSGLW0AK/WF/l20mcVr43mmDm2DqYhAcpJATmJeqMEBn1jfJB+Rnx6V7kbwqdDpbXP5eQQ7ZGngEyw8qx3RQhGj9QWs+UYNVmnbocQuoukOjtbYooiVUmU= \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ad9f9a..37b6970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.0.1 (2019-10-20) + +- Fix authentication +- Add a CoreService aggregating all the internal services + # 1.0.0 (2019-10-16) - Backport existing services from @plone/restapi-angular diff --git a/projects/grange-core/package.json b/projects/grange-core/package.json index 471d443..e06fc2f 100644 --- a/projects/grange-core/package.json +++ b/projects/grange-core/package.json @@ -1,6 +1,6 @@ { "name": "grange-core", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "author": { "name": "Eric Brehault", diff --git a/projects/grange-core/src/index.ts b/projects/grange-core/src/index.ts index a3d8d6f..1d7fbd7 100644 --- a/projects/grange-core/src/index.ts +++ b/projects/grange-core/src/index.ts @@ -1,3 +1,4 @@ +export { GrangeCore } from './lib/core.service'; export { APIService } from './lib/api.service'; export { AuthenticationService } from './lib/authentication.service'; export { CacheService } from './lib/cache.service'; diff --git a/projects/grange-core/src/lib/authentication.service.ts b/projects/grange-core/src/lib/authentication.service.ts index 9f4f391..384df21 100644 --- a/projects/grange-core/src/lib/authentication.service.ts +++ b/projects/grange-core/src/lib/authentication.service.ts @@ -6,7 +6,7 @@ import { import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { AuthenticatedStatus, Error, PasswordResetInfo, UserInfoTokenParts, LoginToken } from './interfaces'; -import { tap, catchError } from 'rxjs/operators'; +import { tap, catchError, map } from 'rxjs/operators'; import { ConfigurationService } from './configuration.service'; @Injectable({ @@ -79,28 +79,33 @@ export class AuthenticationService { }); } + setAuthToken(token: string) { + localStorage.setItem('auth', token); + localStorage.setItem( + 'auth_time', + new Date().toISOString(), + ); + this.isAuthenticated.next({ + state: true, + pending: false, + username: this.getUsername(), + }); + } + login(login: string, password: string, path?: string): Observable { const headers = this.getHeaders(); const body = JSON.stringify({ - login: login, // on plone.restapi login endpoint, username key is login - password: password, + login, + password, }); return this.http .post(this.config.get('BACKEND_URL') + (path || '') + '/@login', body, { - headers: headers, + headers, }).pipe( - tap((data: LoginToken) => { + map((data: LoginToken) => { if (data.token) { - localStorage.setItem('auth', data['token']); - localStorage.setItem( - 'auth_time', - new Date().toISOString(), - ); - this.isAuthenticated.next({ - state: true, - pending: false, - username: this.getUsername(), - }); + this.setAuthToken(data.token); + return true; } else { localStorage.removeItem('auth'); localStorage.removeItem('auth_time'); @@ -109,6 +114,7 @@ export class AuthenticationService { pending: false, username: null, }); + return false; } }), catchError((errorResponse: HttpErrorResponse) => { diff --git a/projects/grange-core/src/lib/core.service.ts b/projects/grange-core/src/lib/core.service.ts new file mode 100644 index 0000000..19ef946 --- /dev/null +++ b/projects/grange-core/src/lib/core.service.ts @@ -0,0 +1,20 @@ +import { Injectable } from '@angular/core'; +import { APIService } from './api.service'; +import { AuthenticationService } from './authentication.service'; +import { CacheService } from './cache.service'; +import { LoadingService } from './loading.service'; +import { ResourceService } from './resource.service'; + +@Injectable({ + providedIn: 'root' +}) +export class GrangeCore { + + constructor( + public api: APIService, + public auth: AuthenticationService, + public cache: CacheService, + public loading: LoadingService, + public resource: ResourceService + ) { } +} diff --git a/projects/grange-core/src/lib/interfaces.ts b/projects/grange-core/src/lib/interfaces.ts index f123e22..b4810a0 100644 --- a/projects/grange-core/src/lib/interfaces.ts +++ b/projects/grange-core/src/lib/interfaces.ts @@ -118,4 +118,14 @@ export interface SearchResults { items_total: number; items: any[]; batching: Batching; -} \ No newline at end of file +} + +export interface Resource { + '@id': string; + '@name': string; + '@type': string; + '@uid': string; + title: string; + description?: string; + is_folderish: boolean; +} diff --git a/projects/grange-core/src/lib/resource.service.ts b/projects/grange-core/src/lib/resource.service.ts index 96596da..70f2827 100644 --- a/projects/grange-core/src/lib/resource.service.ts +++ b/projects/grange-core/src/lib/resource.service.ts @@ -8,6 +8,7 @@ import { WorkflowHistoryItem, WorkflowInformation, WorkflowTransitionOptions, + Resource, } from './interfaces'; import { Vocabulary } from './vocabularies'; import { APIService } from './api.service'; @@ -193,7 +194,7 @@ export class ResourceService { return this.cache.get(path); } - items(path: string, page = 1): Observable { + items(path: string, page = 1): Observable<{items: Resource[], page: number, page_size: number, total: number}> { return this.cache.get(`${path}/@items?page=${page}`); }