Added basic auth guard
parent
033f9de439
commit
ecc8a61102
@ -0,0 +1,30 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import {
|
||||
Router,
|
||||
CanActivate,
|
||||
ActivatedRouteSnapshot,
|
||||
RouterStateSnapshot
|
||||
} from "@angular/router";
|
||||
import { AuthUser } from '../models/user';
|
||||
|
||||
import { AuthService } from "../services/auth.service";
|
||||
|
||||
@Injectable({ providedIn: "root" })
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private router: Router,
|
||||
private authService: AuthService
|
||||
) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||
const currentUser = this.authService.getCurrentUserValue;
|
||||
if (Object.keys(currentUser).length != 0) {
|
||||
// authorised so return true
|
||||
return true;
|
||||
}
|
||||
|
||||
// not logged in so redirect to login page with the return url
|
||||
this.router.navigate(["/login"], { queryParams: { returnUrl: state.url } });
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { catchError, finalize, map, tap } from 'rxjs/operators';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { Observable, BehaviorSubject, of } from 'rxjs';
|
||||
import { User } from "../models/user";
|
||||
import { AuthService} from "./auth.service";
|
||||
import { ObserveOnSubscriber } from 'rxjs/internal/operators/observeOn';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RoleService {
|
||||
|
||||
constructor(private http: HttpClient, private authService: AuthService) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public getAllUsers(): Observable<User[]> {
|
||||
return this.http.get<User[]>(`${environment.authUrl}/users`);
|
||||
}
|
||||
|
||||
public getUser(email: string): Observable<User> {
|
||||
return this.http.get<User>(`${environment.authUrl}/users/${email}`);
|
||||
}
|
||||
|
||||
public getUserPermissions(email: string): Observable<any> {
|
||||
return this.http.get<any>(`${environment.authUrl}/users/${email}/permissions`)
|
||||
}
|
||||
|
||||
public updateUser(user: User): Observable<User> {
|
||||
return this.http.post<User>(`${environment.authUrl}/users/${user.email}/update`, user);
|
||||
}
|
||||
|
||||
public deleteUser(email: string): Observable<any> {
|
||||
return this.http.delete<any>(`${environment.authUrl}/users/` + email + "/delete");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue