Last active
May 31, 2023 20:10
-
-
Save alieslamifard/dd81ce85e20dc47c57ed6825ff153288 to your computer and use it in GitHub Desktop.
Revisions
-
alieslamifard revised this gist
Oct 25, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ export default WrappedComponent => { Router.replace(login); } } else if (WrappedComponent.getInitialProps) { const wrappedProps = await WrappedComponent.getInitialProps({...context, auth: userAuth}); return { ...wrappedProps, userAuth }; } -
alieslamifard revised this gist
Oct 25, 2020 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,22 +15,22 @@ const checkUserAuthentication = () => { export default WrappedComponent => { const hocComponent = ({ ...props }) => <WrappedComponent {...props} />; hocComponent.getInitialProps = async (context) => { const userAuth = await checkUserAuthentication(); // Are you an authorized user or not? if (!userAuth?.auth) { // Handle server-side and client-side rendering. if (context.res) { context.res?.writeHead(302, { Location: login, }); context.res?.end(); } else { Router.replace(login); } } else if (WrappedComponent.getInitialProps) { const wrappedProps = await WrappedComponent.getInitialProps(context); return { ...wrappedProps, userAuth }; } -
alieslamifard revised this gist
Jul 2, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -22,10 +22,10 @@ export default WrappedComponent => { if (!userAuth?.auth) { // Handle server-side and client-side rendering. if (res) { res?.writeHead(302, { Location: login, }); res?.end(); } else { Router.replace(login); } -
alieslamifard created this gist
Mar 24, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ import React from 'react'; import Router from 'next/router'; const login = '/login?redirected=true'; // Define your login route address. /** * Check user authentication and authorization * It depends on you and your auth service provider. * @returns {{auth: null}} */ const checkUserAuthentication = () => { return { auth: null }; // change null to { isAdmin: true } for test it. }; export default WrappedComponent => { const hocComponent = ({ ...props }) => <WrappedComponent {...props} />; hocComponent.getInitialProps = async ({ res }) => { const userAuth = await checkUserAuthentication(); // Are you an authorized user or not? if (!userAuth?.auth) { // Handle server-side and client-side rendering. if (res) { res.writeHead(302, { Location: login, }); res.end(); } else { Router.replace(login); } } else if (WrappedComponent.getInitialProps) { const wrappedProps = await WrappedComponent.getInitialProps(userAuth); return { ...wrappedProps, userAuth }; } return { userAuth }; }; return hocComponent; };