src/app/services/auth.service.ts
Properties |
|
Methods |
|
Accessors |
constructor(http: HttpClient, router: Router, storage: StorageService, sessionStore: SessionStore, errorStore: ErrorStore, helper: JwtHelperService, featureFlagService: FeatureFlagService)
|
||||||||||||||||||||||||
Defined in src/app/services/auth.service.ts:18
|
||||||||||||||||||||||||
Parameters :
|
Private fallbackToken |
fallbackToken()
|
Defined in src/app/services/auth.service.ts:74
|
Returns :
string
|
getToken |
getToken()
|
Defined in src/app/services/auth.service.ts:70
|
Returns :
string
|
login | ||||||
login(credentials: Credentials)
|
||||||
Defined in src/app/services/auth.service.ts:40
|
||||||
Parameters :
Returns :
Observable<boolean>
|
logout |
logout()
|
Defined in src/app/services/auth.service.ts:64
|
Returns :
void
|
Private token |
token:
|
Type : string
|
Defined in src/app/services/auth.service.ts:18
|
authenticated |
getauthenticated()
|
Defined in src/app/services/auth.service.ts:30
|
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { JwtHelperService } from '@auth0/angular-jwt';
import { Observable } from 'rxjs/Observable';
import { AUTH_API, JWT_KEY, RT247_FLAG } from '../app.constants';
import { AuthResponse } from '../models/auth-response.model';
import { Credentials } from '../models/credentials.model';
import { IJwt } from '../models/i-jwt.model';
import { ErrorStore } from '../stores/error.store';
import { SessionStore } from '../stores/session.store';
import { StorageService } from './storage.service';
import { map, catchError } from 'rxjs/operators';
import { FeatureFlagService } from './feature-flag.service';
@Injectable()
export class AuthService {
private token: string;
constructor(
private http: HttpClient,
private router: Router,
private storage: StorageService,
private sessionStore: SessionStore,
private errorStore: ErrorStore,
private helper: JwtHelperService,
private featureFlagService: FeatureFlagService
) {}
get authenticated(): boolean {
try {
const rawToken = this.getToken();
const isExpired = this.helper.isTokenExpired(rawToken);
return !isExpired;
} catch (error) {
return false;
}
}
login(credentials: Credentials): Observable<boolean> {
return this.http.post<AuthResponse>(`${AUTH_API}/login`, credentials).pipe(
map((res) => {
const authenticated = res.token && res.token.length > 0;
if (authenticated) {
this.storage.set(JWT_KEY, res.token);
const jwt = this.helper.decodeToken(res.token) as IJwt;
//RealTime247 Feature Flag
this.storage.set(RT247_FLAG, "false");
this.featureFlagService.getFeatureFlag(jwt.domain).subscribe((data: any) => {
this.storage.set(RT247_FLAG, data.IsOn);
});
this.sessionStore.createSession(jwt);
}
return authenticated;
}),
catchError((err) => {
return Observable.of(false);
})
);
}
logout(): void {
this.sessionStore.clearSession();
this.http.get(`${AUTH_API}/signout`).subscribe(() => {});
this.router.navigate(['/login']);
}
getToken(): string {
return this.token ? this.token : this.fallbackToken();
}
private fallbackToken(): string {
return this.storage.get(JWT_KEY);
}
}