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) {}
|
||||
|
||||
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(
|
||||
map((user: User) => {
|
||||
if (user) {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
|
|||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
|
||||
import { ToastrService } from 'ngx-toastr';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { map, take } from 'rxjs/operators';
|
||||
import { User } from '../_models/user';
|
||||
import { AccountService } from '../_services/account.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) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
||||
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
|
||||
|
||||
const libraryId = parseInt(state.url.split('library/')[1], 10);
|
||||
return this.memberService.hasLibraryAccess(libraryId);
|
||||
|
||||
|
||||
|
||||
|
||||
return this.accountService.currentUser$.pipe(
|
||||
map((user: User) => {
|
||||
if (user) {
|
||||
const libraryId = parseInt(state.url.split('library/')[1], 10);
|
||||
this.memberService.hasLibraryAccess(libraryId).pipe(res => {
|
||||
console.log('return: ', res);
|
||||
return res;
|
||||
});
|
||||
console.log('state:', state.url);
|
||||
console.log('route: ', route);
|
||||
return true;
|
||||
}
|
||||
this.toastr.error('You are not authorized to view this page.');
|
||||
return false;
|
||||
})
|
||||
);
|
||||
// return this.accountService.currentUser$.pipe(
|
||||
// take((user: User) => {
|
||||
// if (user) {
|
||||
// const libraryId = parseInt(state.url.split('library/')[1], 10);
|
||||
// return this.memberService.hasLibraryAccess(libraryId);
|
||||
// //return true;
|
||||
// }
|
||||
// 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 { LibraryComponent } from './library/library.component';
|
||||
import { SeriesDetailComponent } from './series-detail/series-detail.component';
|
||||
import { AuthGuard } from './_guards/auth.guard';
|
||||
import { LibraryAccessGuard } from './_guards/library-access.guard';
|
||||
|
||||
|
||||
|
|
@ -17,7 +18,7 @@ const routes: Routes = [
|
|||
{
|
||||
path: '',
|
||||
runGuardsAndResolvers: 'always',
|
||||
canActivate: [LibraryAccessGuard],
|
||||
canActivate: [AuthGuard, LibraryAccessGuard],
|
||||
children: [
|
||||
{path: 'library/:id', component: LibraryDetailComponent},
|
||||
{path: 'library/:id/series/:id', component: SeriesDetailComponent},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue