Ensure users are logged in before letting them access library route
This commit is contained in:
parent
bdf382ca14
commit
cb9e88acdc
3 changed files with 18 additions and 23 deletions
|
|
@ -13,7 +13,7 @@ export class AuthGuard implements CanActivate {
|
||||||
constructor(private accountService: AccountService, private toastr: ToastrService) {}
|
constructor(private accountService: AccountService, private toastr: ToastrService) {}
|
||||||
|
|
||||||
canActivate(): Observable<boolean> {
|
canActivate(): Observable<boolean> {
|
||||||
// this automaticallys subs due to being router guard
|
// TODO: on failure, can we bring them back to home to show login screen
|
||||||
return this.accountService.currentUser$.pipe(
|
return this.accountService.currentUser$.pipe(
|
||||||
map((user: User) => {
|
map((user: User) => {
|
||||||
if (user) {
|
if (user) {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
|
||||||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
|
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
|
||||||
import { ToastrService } from 'ngx-toastr';
|
import { ToastrService } from 'ngx-toastr';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map, take } from 'rxjs/operators';
|
||||||
import { User } from '../_models/user';
|
import { User } from '../_models/user';
|
||||||
import { AccountService } from '../_services/account.service';
|
import { AccountService } from '../_services/account.service';
|
||||||
import { MemberService } from '../_services/member.service';
|
import { MemberService } from '../_services/member.service';
|
||||||
|
|
@ -14,28 +14,22 @@ export class LibraryAccessGuard implements CanActivate {
|
||||||
|
|
||||||
constructor(private accountService: AccountService, private toastr: ToastrService, private memberService: MemberService) {}
|
constructor(private accountService: AccountService, private toastr: ToastrService, private memberService: MemberService) {}
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return this.accountService.currentUser$.pipe(
|
|
||||||
map((user: User) => {
|
|
||||||
if (user) {
|
|
||||||
const libraryId = parseInt(state.url.split('library/')[1], 10);
|
const libraryId = parseInt(state.url.split('library/')[1], 10);
|
||||||
this.memberService.hasLibraryAccess(libraryId).pipe(res => {
|
return this.memberService.hasLibraryAccess(libraryId);
|
||||||
console.log('return: ', res);
|
|
||||||
return res;
|
// return this.accountService.currentUser$.pipe(
|
||||||
});
|
// take((user: User) => {
|
||||||
console.log('state:', state.url);
|
// if (user) {
|
||||||
console.log('route: ', route);
|
// const libraryId = parseInt(state.url.split('library/')[1], 10);
|
||||||
return true;
|
// return this.memberService.hasLibraryAccess(libraryId);
|
||||||
}
|
// //return true;
|
||||||
this.toastr.error('You are not authorized to view this page.');
|
// }
|
||||||
return false;
|
// this.toastr.error('You are not authorized to view this page.');
|
||||||
})
|
// return false;
|
||||||
);
|
// })
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { HomeComponent } from './home/home.component';
|
||||||
import { LibraryDetailComponent } from './library-detail/library-detail.component';
|
import { LibraryDetailComponent } from './library-detail/library-detail.component';
|
||||||
import { LibraryComponent } from './library/library.component';
|
import { LibraryComponent } from './library/library.component';
|
||||||
import { SeriesDetailComponent } from './series-detail/series-detail.component';
|
import { SeriesDetailComponent } from './series-detail/series-detail.component';
|
||||||
|
import { AuthGuard } from './_guards/auth.guard';
|
||||||
import { LibraryAccessGuard } from './_guards/library-access.guard';
|
import { LibraryAccessGuard } from './_guards/library-access.guard';
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -17,7 +18,7 @@ const routes: Routes = [
|
||||||
{
|
{
|
||||||
path: '',
|
path: '',
|
||||||
runGuardsAndResolvers: 'always',
|
runGuardsAndResolvers: 'always',
|
||||||
canActivate: [LibraryAccessGuard],
|
canActivate: [AuthGuard, LibraryAccessGuard],
|
||||||
children: [
|
children: [
|
||||||
{path: 'library/:id', component: LibraryDetailComponent},
|
{path: 'library/:id', component: LibraryDetailComponent},
|
||||||
{path: 'library/:id/series/:id', component: SeriesDetailComponent},
|
{path: 'library/:id/series/:id', component: SeriesDetailComponent},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue