Skip to content

Instantly share code, notes, and snippets.

@alieslamifard
Last active May 31, 2023 20:10
Show Gist options
  • Select an option

  • Save alieslamifard/dd81ce85e20dc47c57ed6825ff153288 to your computer and use it in GitHub Desktop.

Select an option

Save alieslamifard/dd81ce85e20dc47c57ed6825ff153288 to your computer and use it in GitHub Desktop.

Revisions

  1. alieslamifard revised this gist Oct 25, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion withPrivateRoute.jsx
    Original 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);
    const wrappedProps = await WrappedComponent.getInitialProps({...context, auth: userAuth});
    return { ...wrappedProps, userAuth };
    }

  2. alieslamifard revised this gist Oct 25, 2020. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions withPrivateRoute.jsx
    Original 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 ({ res }) => {
    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 (res) {
    res?.writeHead(302, {
    if (context.res) {
    context.res?.writeHead(302, {
    Location: login,
    });
    res?.end();
    context.res?.end();
    } else {
    Router.replace(login);
    }
    } else if (WrappedComponent.getInitialProps) {
    const wrappedProps = await WrappedComponent.getInitialProps(userAuth);
    const wrappedProps = await WrappedComponent.getInitialProps(context);
    return { ...wrappedProps, userAuth };
    }

  3. alieslamifard revised this gist Jul 2, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions withPrivateRoute.jsx
    Original 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, {
    res?.writeHead(302, {
    Location: login,
    });
    res.end();
    res?.end();
    } else {
    Router.replace(login);
    }
  4. alieslamifard created this gist Mar 24, 2020.
    41 changes: 41 additions & 0 deletions withPrivateRoute.jsx
    Original 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;
    };