-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added route gaurd for frontend access control
- Loading branch information
1 parent
4312f7d
commit 6c58a0e
Showing
2 changed files
with
58 additions
and
0 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
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,51 @@ | ||
'use client'; | ||
import { USER, IS_LOGIN } from '../lib/constants' | ||
|
||
export const RouteGuard = { | ||
|
||
isLogin(){ | ||
if(typeof sessionStorage !== "undefined"){ | ||
return sessionStorage?.getItem(IS_LOGIN) == "true" || false; | ||
} | ||
else{ | ||
return false; | ||
} | ||
}, | ||
loginUser(){ | ||
if(this.isLogin()){ | ||
return JSON.parse(sessionStorage.getItem(USER) || '') as UserProfile; | ||
} | ||
else{ | ||
return null; | ||
} | ||
}, | ||
apply(path: string):boolean{ | ||
if(process.env.NODE_ENV === 'development') return true; | ||
switch(path){ | ||
case '/': | ||
return true; | ||
case '/auth/google': | ||
return true; | ||
case '/auth/access': | ||
return true; | ||
case '/auth/error': | ||
return true; | ||
case '/dashboard': | ||
return true; | ||
case '/quiz': | ||
return this.accessibleBy(['student']); | ||
case '/questions/searchlist': | ||
return this.accessibleBy(['admin', 'tutor']); | ||
case '/questions/topics': | ||
return this.accessibleBy(['admin', 'tutor']); | ||
case '/classes': | ||
return this.accessibleBy(['admin', 'tutor']); | ||
default: | ||
return false; | ||
} | ||
}, | ||
accessibleBy (roles: string[]) { | ||
return this.isLogin() && this.loginUser()?.roles.some(role => roles.includes(role)) || false; | ||
}, | ||
|
||
} |