-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement guards to disallow certain pages based on login state
Closes #37
- Loading branch information
Showing
4 changed files
with
67 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {CanActivateFn, Router} from "@angular/router"; | ||
import {inject} from "@angular/core"; | ||
import {AuthService} from "../auth.service"; | ||
import {BannerService} from "../../banners/banner.service"; | ||
import {ExtendedUser} from "../types/extended-user"; | ||
import {UserRoles} from "../types/user-roles"; | ||
|
||
export const adminAuthenticationGuard: CanActivateFn = () => { | ||
const user: ExtendedUser | undefined = inject(AuthService).user; | ||
if(!user || user.role < UserRoles.Admin) { | ||
inject(Router).navigate(['/']); | ||
inject(BannerService).pushError("Unauthorized", "You lack the permissions to view this page.") | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {CanActivateFn, Router} from "@angular/router"; | ||
import {AuthService} from "../auth.service"; | ||
import {inject} from "@angular/core"; | ||
import {BannerService} from "../../banners/banner.service"; | ||
|
||
// Adapted from SoundShapes-web | ||
// https://github.com/turecross321/soundshapes-web/blob/master/src/app/auth/auth.guard.ts | ||
|
||
export const authenticationGuard: CanActivateFn = () => { | ||
if(!inject(AuthService).user) { | ||
inject(Router).navigate(['/login']); | ||
inject(BannerService).pushWarning("Not logged in", "This page requires that you log in or register.") | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {CanActivateFn, Router} from "@angular/router"; | ||
import {ExtendedUser} from "../types/extended-user"; | ||
import {inject} from "@angular/core"; | ||
import {AuthService} from "../auth.service"; | ||
|
||
export const noAuthenticationGuard: CanActivateFn = () => { | ||
const user: ExtendedUser | undefined = inject(AuthService).user; | ||
if(user) { | ||
inject(Router).navigate(['/']); | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters