Added basic auth guard

pull/6/head
FlayInAHook 4 years ago
parent 033f9de439
commit ecc8a61102

@ -10,18 +10,19 @@ import { EquipmentComponent } from './pages/tables/equipment/equipment.component
import { LendingStationsComponent } from './pages/tables/lending-stations/lending-stations.component'; import { LendingStationsComponent } from './pages/tables/lending-stations/lending-stations.component';
import { ParticipantsComponent } from './pages/tables/participants/participants.component'; import { ParticipantsComponent } from './pages/tables/participants/participants.component';
import { TimeFramesComponent } from './pages/tables/time-frames/time-frames.component'; import { TimeFramesComponent } from './pages/tables/time-frames/time-frames.component';
import {AuthGuard} from './helper/auth.guard';
const routes: Routes = [ const routes: Routes = [
{ path: 'login', component: LoginComponent }, { path: 'login', component: LoginComponent },
{ path: 'tableOverview', component: TableOverviewComponent }, { path: 'tableOverview', component: TableOverviewComponent, canActivate: [AuthGuard]},
{ path: 'table/bikes', component: BikesComponent }, { path: 'table/bikes', component: BikesComponent, canActivate: [AuthGuard] },
{ path: 'bike/:id', component: BikeComponent }, { path: 'bike/:id', component: BikeComponent, canActivate: [AuthGuard] },
{ path: 'table/participants', component: ParticipantsComponent }, { path: 'table/participants', component: ParticipantsComponent, canActivate: [AuthGuard] },
{ path: 'table/lendingStations', component: LendingStationsComponent }, { path: 'table/lendingStations', component: LendingStationsComponent, canActivate: [AuthGuard] },
{ path: 'table/equipmentTypes', component: EquipmentTypesComponent }, { path: 'table/equipmentTypes', component: EquipmentTypesComponent, canActivate: [AuthGuard] },
{ path: 'table/engagementTypes', component: EngagementTypesComponent }, { path: 'table/engagementTypes', component: EngagementTypesComponent, canActivate: [AuthGuard] },
{ path: 'table/equipment', component: EquipmentComponent }, { path: 'table/equipment', component: EquipmentComponent, canActivate: [AuthGuard] },
{ path: 'table/timeFrames', component: TimeFramesComponent }, { path: 'table/timeFrames', component: TimeFramesComponent, canActivate: [AuthGuard] },
{ path: '', redirectTo: 'tableOverview', pathMatch: 'full' }, { path: '', redirectTo: 'tableOverview', pathMatch: 'full' },
{ path: 'table', redirectTo: 'tableOverview', pathMatch: 'full' }, { path: 'table', redirectTo: 'tableOverview', pathMatch: 'full' },
{ path: '**', redirectTo: 'tableOverview' }, { path: '**', redirectTo: 'tableOverview' },

@ -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…
Cancel
Save