Да .. есть AuthService:
import { SessionService } from '../session/session.service';
import { environment } from './../../../environments/environment';
import { IUser, UserType } from '../../../../../contracts/IUser';
import { IEmployee } from '../../../../../contracts/IEmployee';
import { Injectable,Inject } from '@angular/core';
import { Router } from '@angular/router';
import 'rxjs/add/operator/filter';
import * as auth0 from 'auth0-js';
import { Http } from '@angular/http';
import {ReplaySubject, Observable} from 'rxjs/Rx';
import { ApiService } from '../api/api.service';
import { ActivityService } from '../activity/activity.service';
import { UserActivityAction } from '../../../../../contracts/Library';
import { ApiRoutes } from '../../../../../contracts/ApiRoutes';
import { AlertService} from '../alert/alert.service';
import { MessageService} from '../message/message.service';
import {Events} from '../../../../../contracts/Library';
import { map } from 'rxjs/operators';
@Injectable()
export class AuthService {
private userIsLoggedIn : boolean;
constructor(public http: ApiService,private rHttp: Http,
private session:SessionService,
@Inject('BASE_URL') public baseUrl: string,
public router: Router,
private alertService: AlertService,
private activity: ActivityService,
private messageService : MessageService)
{
this.session = session;
}
// logIn attempts to log in a user
handleAuthentication() : void {
let auth = this.http.httpGetAll(ApiRoutes.AuthRoute);
auth.subscribe(
data => {
let results = data.json();
switch(results.status) {
case 401: {
this.routeAfterLogin(false);
this.messageService.sendMessage('',Events.UserLogout);
break;
}
case 402: {
break;
}
case 200: {
//when success
break;
}
default: {
this.routeAfterLogin(false);
break;
}
}
},
error => {
console.log(error);
});
this.router.navigate(['/home']);
}
public login(data): Observable<any> {
return this.http.httpPost(ApiRoutes.LoginRoute,data).pipe(map(result=>{
if (result && result.status == 200) {
//when success
}else{
let msg = "some error message";
this.routeAfterLogin(false);
return {msg:msg};
}
}));
}
private routeAfterLogin(state:boolean) {
this.userIsLoggedIn = state;
if(this.userIsLoggedIn){
this.router.navigate(['/home']);
console.log("Login");
} else {
this.router.navigate(['/login']);
console.log("Failure Login");
}
}
public logout(): void {
let auth = this.http.httpGetAll(ApiRoutes.LogoutRoute);
auth.subscribe(
data => {
console.log("Logout");
this.messageService.sendMessage('',Events.UserLogout);
this.router.navigate(['/login']);
},
error => {
console.log(error);
});
}
public isAuthenticated(): boolean {
return this.userIsLoggedIn;
}
public fillFileDownloadPass(){
let getUploadPath$ = this.http.httpGetAll(ApiRoutes.DownloaPathdRout);
getUploadPath$.subscribe(
data => {
let results = data.json();
switch(results.status) {
case 200: {
this.session.download101AtchFilePath = results.filehDownloadPat;
this.session.approvedFormsPath = results.approvedFormsPath;
break;
}
case 400: {
console.log("didn't find an upload path");
break;
}
default: {
break;
}
}
},
error => {
console.log(error);
});
}
}
Функция handleAuthentication () вызывается из app.component ..
ngOnInit() {
this.authService.handleAuthentication();
this.subscription = this.messageService.getMessage().subscribe(message => {
switch (message.type) {
case Events.UserLogin:
this.showNav = true;
break;
case Events.UserLogout:
this.showNav = false;
break;
case Events.FirstEntrance :
//some message
break;
}
});
}
Функция httpPost ApiService оборачивает http:
httpPost(url:string,data:any):Observable<Response | any> {
return this._http.post(`${this.baseUrl}${url}`, data )
.map(this.parseData)
.catch(this.handleError.bind(this));
}
private parseData(res: Response) {
return res.json() || [];
}