Created
December 18, 2020 16:34
-
-
Save createdbymahmood/e7082abeba71b525ec660f936868a208 to your computer and use it in GitHub Desktop.
acl for pages in next js
This file contains hidden or 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
export const accesKeys = { | |
guest: { | |
home: true, | |
users: true, | |
user: true, | |
}, | |
}; |
This file contains hidden or 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
import { ComponentType } from "react"; | |
import { accesKeys } from "constants/accesKeys"; | |
import { useRouter } from "next/router"; | |
import { useLayoutEffectOnce } from "helpers/useEffectOnce"; | |
import get from "lodash/get"; | |
export const injectAuthConfig = (accessKey: string) => { | |
return function <T>(Component: ComponentType<T>) { | |
return function (props: T): JSX.Element { | |
console.log(accessKey); | |
const hasAccess = get(accesKeys.guest, accessKey); | |
if (hasAccess === undefined) { | |
throw new Error( | |
"that access key shit must be correctly provided" | |
); | |
} | |
const router = useRouter(); | |
useLayoutEffectOnce(() => { | |
if (!hasAccess) { | |
router.push("/"); | |
} | |
}); | |
return <Component {...props} />; | |
}; | |
}; | |
}; |
This file contains hidden or 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
export default compose( | |
injectAuthConfig("home"), | |
)(Index); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment