|
- import {EventEmitter, Injectable, OnDestroy, OnInit} from '@angular/core';
- import { NotificationService } from '@progress/kendo-angular-notification';
- import {HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
- import {apiV1LoginResponse} from '../models/api-v1-login-response';
- import {Observable} from 'rxjs';
- import {AppComponent} from '../app.component';
- import {Router} from '@angular/router';
-
- @Injectable()
- export class AuthService {
-
- public apiUrl = 'https://svr2021.lawipac.com:8080/api/v1/';
- public apiWsUrl = 'wss://svr2021.lawipac.com:8080/api/v1/ws';
- public loggedIn = new apiV1LoginResponse(false, '', '',0 );
- loginSuccess = new EventEmitter <apiV1LoginResponse>();
-
- constructor( private http: HttpClient ,
- private router: Router) {
- }
-
-
- // tslint:disable-next-line:typedef
- public AutoLogin() {
- const sfm: apiV1LoginResponse = JSON.parse(localStorage.getItem('sfm'));
- if (!sfm) {
- console.log('no auto login');
- return;
- }
- this.loggedIn = new apiV1LoginResponse(
- sfm.login,
- sfm.machineId,
- sfm.session,
- sfm.sessionExpire
- );
- this.loginSuccess.emit(this.loggedIn);
- //console.log ( 'auto login emit events', this.loggedIn);
- }
-
- // tslint:disable-next-line:typedef
- isAuthenticated() {
- return this.loggedIn.login;
- }
-
- allowEditLoan(): boolean{
- return true;
- }
-
- // tslint:disable-next-line:typedef
- login(email: string, password: string) {
-
- this.http.post<apiV1LoginResponse>(this.apiUrl + 'login', {u: email, p: password}).subscribe(
- responseData => {
- this.loggedIn.session = responseData['Biukop-Session'];
- this.loggedIn.login = responseData.login;
- this.loggedIn.machineId = responseData['Biukop-Mid'];
- this.loggedIn.sessionExpire = responseData.sessionExpire;
- localStorage.setItem('sfm', JSON.stringify(this.loggedIn));
- localStorage.setItem('mid', this.loggedIn.machineId);
- this.loginSuccess.emit(responseData);
- },
- error => {
- const fail = new apiV1LoginResponse(false,'','',0);
- this.loggedIn = fail;
- console.log('login error', error);
- this.loginSuccess.emit(this.loggedIn);
- }
-
- );
- }
-
- // tslint:disable-next-line:typedef
- logout() {
- this.loggedIn = new apiV1LoginResponse(false,'','',0);
- localStorage.removeItem('sfm');
- this.loginSuccess.emit(this.loggedIn);
- this.router.navigate(['/login']).then(r =>{
- console.log('prepare to log back in');
- } );
- this.http.post(`${this.apiUrl}logout`, '').subscribe(
- resp => {
- console.log('successfully logout from server');
- },
- event => {
- console.log('error logout from server', event);
- }
- );
- }
-
-
- // tslint:disable-next-line:variable-name
- public getUrl(key: string): string{
- const s = this.apiUrl + key;
- const kvPair: {key: string, value: string}[] = [
- {key: 'login', value: this.apiUrl + 'login'},
- {key: 'logout', value: this.apiUrl + 'logout'}
- ];
-
- kvPair.forEach( item => {
- if ( item.key === key) {
- return item.value;
- }
- });
-
- // not found if arrive here
- return s;
- }}
|