File tree 3 files changed +21
-18
lines changed
3 files changed +21
-18
lines changed Original file line number Diff line number Diff line change 1
1
import React , { ReactNode , useEffect } from 'react'
2
2
3
3
import { NextShieldProps } from '../types/props'
4
- import { verifyPath } from '../libs/routes'
4
+ import { getAccessRoute , verifyPath } from '../libs/routes'
5
5
6
6
/**
7
7
* 😉 The shield that every Next.js project needs
@@ -66,23 +66,11 @@ export function NextShield<
66
66
67
67
useEffect ( ( ) => {
68
68
if ( ! isAuth && ! isLoading && pathIsPrivate ) replace ( loginRoute )
69
- if ( isAuth && ! isLoading && pathIsPublic ) {
70
- if ( typeof accessRoute !== 'undefined' ) {
71
- replace ( accessRoute )
72
- }
73
- if ( RBAC && userRole ) {
74
- replace ( RBAC [ userRole ] . accessRoute )
75
- }
76
- }
69
+ if ( isAuth && ! isLoading && pathIsPublic )
70
+ getAccessRoute ( RBAC , userRole , accessRoute , loginRoute )
77
71
78
- if ( isAuth && userRole && ! isLoading && ! pathIsHybrid && ! pathIsAuthorized ) {
79
- if ( typeof accessRoute !== 'undefined' ) {
80
- replace ( accessRoute )
81
- }
82
- if ( RBAC && userRole ) {
83
- replace ( RBAC [ userRole ] . accessRoute )
84
- }
85
- }
72
+ if ( isAuth && userRole && ! isLoading && ! pathIsHybrid && ! pathIsAuthorized )
73
+ getAccessRoute ( RBAC , userRole , accessRoute , loginRoute )
86
74
} , [
87
75
replace ,
88
76
userRole ,
Original file line number Diff line number Diff line change
1
+ import { RoleAccess } from '../types/props'
2
+
1
3
export function verifyPath ( routes : string [ ] | undefined , uri : string ) {
2
4
return routes ?. some ( route => route === uri )
3
5
}
6
+
7
+ export function getAccessRoute (
8
+ RBAC : RoleAccess < string [ ] > | undefined ,
9
+ userRole : string | undefined ,
10
+ accessRoute : string | undefined ,
11
+ loginRoute : string
12
+ ) {
13
+ if ( typeof accessRoute !== 'undefined' ) return accessRoute
14
+
15
+ if ( RBAC && userRole ) return RBAC [ userRole ] . accessRoute
16
+
17
+ return loginRoute
18
+ }
Original file line number Diff line number Diff line change 1
1
import type { ReactNode } from "react" ;
2
2
import type { NextRouter } from 'next/router'
3
3
4
- type RoleAccess < Routes extends string [ ] > = {
4
+ export type RoleAccess < Routes extends string [ ] > = {
5
5
[ index : string ] : {
6
6
grantedRoutes : Routes ,
7
7
accessRoute : Routes [ number ]
You can’t perform that action at this time.
0 commit comments