1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
18 import { Injectable, OnInit } from '@angular/core';
\r
20 import * as feathers from '@feathersjs/client';
\r
21 import * as io from 'socket.io-client';
\r
22 import * as socketio from '@feathersjs/socketio-client';
\r
23 import * as authentication from '@feathersjs/authentication-client';
\r
24 import { Observable, from, interval } from 'rxjs';
\r
25 import { now } from 'moment';
\r
26 import { AppGlobals } from 'app/app.global';
\r
27 import { CookieService } from 'ngx-cookie-service';
\r
28 import { Router } from '@angular/router';
\r
34 export class FeathersService {
\r
35 // There are no proper typings available for feathers, due to its plugin-heavy nature
\r
36 private _feathers: any;
\r
37 public _socket: any;
\r
38 public auth: Observable<Object>;
\r
40 constructor(private route: Router) {
\r
41 this._socket = io('/',{
\r
42 transports: ['websocket']
\r
43 }); // init socket.io
\r
44 this._socket.on('connect_error', function(data){
\r
45 route.navigateByUrl('/login');
\r
47 this._feathers = feathers(); // init Feathers // add hooks plugin
\r
48 this._feathers.configure(socketio(this._socket, {
\r
50 })); // add socket.io plugin
\r
51 this._feathers.configure(authentication({
\r
52 storage: window.localStorage,
\r
53 storageKey: 'access_token'
\r
56 //set observiable for services to check before calling the service
\r
57 this.auth = from(this._feathers.authenticate());
\r
62 public service(name: string) {
\r
63 return this._feathers.service(name);
\r
67 return this._socket;
\r
70 // expose authentication
\r
71 public authenticate(credentials?): Observable<Object> {
\r
72 return new Observable(observer => {
\r
73 this.auth = from(this._feathers.authenticate(credentials).then(res => {
\r
76 observer.error(err);
\r
77 this.route.navigate(['/login'])
\r
85 return this._feathers.logout();
\r