Skip to content

Commit 3160a73

Browse files
committed
fix(rbac): added type check for userRole
1 parent c945a06 commit 3160a73

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

example/ts-fire-example/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/ts-fire-example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"firebase": "^8.7.0",
1313
"next": "11.0.1",
14-
"next-shield": "^2.0.1",
14+
"next-shield": "^2.0.2",
1515
"react": "17.0.2",
1616
"react-dom": "17.0.2",
1717
"react-firebase-hooks": "^3.0.4",

src/components/NextShield.tsx

+11-7
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,23 @@ export function NextShield<
6363
const pathIsHybrid = verifyPath(hybridRoutes, pathname)
6464
const pathIsAuthorized =
6565
RBAC && userRole && verifyPath(RBAC[userRole].grantedRoutes, pathname)
66-
const route = getAccessRoute(RBAC, userRole, accessRoute, loginRoute)
66+
const access = getAccessRoute(RBAC, userRole, accessRoute, loginRoute)
6767

6868
useEffect(() => {
6969
if (!isAuth && !isLoading && pathIsPrivate) replace(loginRoute)
70-
if (isAuth && !isLoading && pathIsPublic)
71-
replace(route)
72-
73-
if (isAuth && userRole && !isLoading && !pathIsHybrid && !pathIsAuthorized)
74-
replace(route)
70+
if (isAuth && !isLoading && pathIsPublic) replace(access)
71+
if (
72+
isAuth &&
73+
typeof userRole === 'string' &&
74+
!isLoading &&
75+
!pathIsHybrid &&
76+
!pathIsAuthorized
77+
)
78+
replace(access)
7579
}, [
7680
replace,
7781
userRole,
78-
route,
82+
access,
7983
isAuth,
8084
isLoading,
8185
loginRoute,

0 commit comments

Comments
 (0)