1

Hello I am having an issue using some dynamic routes with Next.js. My file is called [type].js. Inside this file I have the following code:

import React from 'react';
import AuthSection from 'components/AuthSection';
import { useRouter } from 'next/router';
import { useAuth } from "../../util/auth";

function AuthPage(props) {
  const router = useRouter();
  const auth = useAuth();
  let getAuthRoles = auth.user ? auth.user.roles[0] : ""
  let pageSrc = ""

  if(getAuthRoles === "Moderation Staff"){
    pageSrc = "/moderation"
  }else if(getAuthRoles === "Super Admin"){
    pageSrc = "/superUser"
  }

  return (
    <AuthSection
      bgColor="default"
      size="medium"
      bgImage=""
      bgImageOpacity={1}
      type={router.query.type}
      afterAuthPath={router.query.next || `${pageSrc}`}
    />
  );

}
  1. I pull some auth roles from Auth0
  2. Using a ternary condition to get the user roles in order to read it and send it to the user
  3. A conditional statement that will redirect the user role page depending on which role the user has
  4. A component that will create the sign-in route, which here I am using the page route.

If I use this:

return (
    <AuthSection
      bgColor="default"
      size="medium"
      bgImage=""
      bgImageOpacity={1}
      type={router.query.type}
      afterAuthPath={router.query.next || '/moderation'}
    />
  );

This works perfectly, but obvious is not what I need.

So basically what I need is, depending on what role the user has, it should redirect to its specific page. So based on my Javascript logic, this would make sense cause I'm adding a dynamic route into the afterAuthPath. But this solution ends up with an error like this:

enter image description here

So I entered in the documentation, and basically, it only shows me how it would work using href but that is not my case.

How can I solve this? I honestly can't think of something else right here. I'll appreciate your help a lot guys!

EDIT: I add my AuthSection.js component below:

import React from "react";
import Section from "components/Section";
import Container from "@material-ui/core/Container";
import SectionHeader from "components/SectionHeader";
import Auth from "components/Auth";

function AuthSection(props) {
  // Values for each auth type
  const allTypeValues = {
    signin: {
      // Top title
      title: "Welcome back",
      // Submit button text
      buttonText: "Sign in",
      // Link text to other auth types
      linkTextSignup: "Create an account",
      linkTextForgotpass: "Forgot Password?",
    },
    signup: {
      title: "Get yourself an account",
      buttonText: "Sign up",
      linkTextSignin: "Sign in",
    },
    forgotpass: {
      title: "Get a new password",
      buttonText: "Reset password",
    },
    changepass: {
      title: "Choose a new password",
      buttonText: "Change password",
    },
  };

  // Ensure we have a valid auth type
  const currentType = allTypeValues[props.type] ? props.type : "signup";

  // Get values for current auth type
  const typeValues = allTypeValues[currentType];

  return (
    <Section
      bgColor={props.bgColor}
      size={props.size}
      bgImage={props.bgImage}
      bgImageOpacity={props.bgImageOpacity}
    >
      <Container maxWidth="xs">
        <SectionHeader
          title={allTypeValues[currentType].title}
          subtitle=""
          size={4}
          textAlign="center"
        />
        <Auth
          type={currentType}
          typeValues={typeValues}
          providers={props.providers}
          afterAuthPath={props.afterAuthPath}
          key={currentType}
        />
      </Container>
    </Section>
  );
}

export default AuthSection;

UPDATE - suggestion code

import React, { useEffect, useState } from "react";
import AuthSection from 'components/AuthSection';
import { useRouter } from 'next/router';
import { useAuth } from "../../util/auth";

function AuthPage(props) {
  const router = useRouter();
  const auth = useAuth();
  const [pageSrc, setPageSrc] = useState("");

  useEffect(() => {
    const getAuthRoles = auth.user ? auth.user.roles[0] : "";
    let newPageSrc = "";
  
    if(getAuthRoles === "Moderation Staff"){
      newPageSrc = "/moderation"
    }else if(getAuthRoles === "Super Admin"){
      newPageSrc = "/superUser"
    }
  
    setPageSrc(newPageSrc);
  }, [auth]);

  return (
    <AuthSection
      bgColor="default"
      size="medium"
      bgImage=""
      bgImageOpacity={1}
      type={router.query.type}
      afterAuthPath={router.query.next || `${pageSrc}`}
    />
  );

}
7
  • 1
    What does your AuthSection component look like? Commented Dec 4, 2021 at 1:46
  • sorry @AliKlein, does AuthSection has something to do with the interpolation? I'm just curious but basically gets all of the Auth services from Auth0, I added my code to my post. Commented Dec 4, 2021 at 2:21
  • 1
    The error occurs because somewhere in your code you're setting an href with the value /auth/[type] (supposedly on a next/link component), but that's not shown anywhere in the code you shared. Commented Dec 4, 2021 at 17:36
  • @juliomalves yeah I do have href but those are into another component of the code that will just render links. (Like Navbar, Footer, etc) What's most weird is that it won't let me do conditional rendering. Commented Dec 4, 2021 at 21:23
  • 1
    where does the afterAuthPath in your AuthSection go? Does it use Link or Router there? If so, are you using an as path prop? Commented Dec 7, 2021 at 15:38

1 Answer 1

1

useRouter is a hook.

how about this code?

const [pageSrc, setPageSrc] = useState("");

useEffect(() => {
  const getAuthRoles = auth.user ? auth.user.roles[0] : "";
  let newPageSrc = "";

  if(getAuthRoles === "Moderation Staff"){
    newPageSrc = "/moderation"
  }else if(getAuthRoles === "Super Admin"){
    newPageSrc = "/superUser"
  }

  setPageSrc(newPageSrc);
}, [auth]);

useRouter is a React Hook, meaning it cannot be used with classes. You can either use withRouter or wrap your class in a function component.

https://nextjs.org/docs/api-reference/next/router#userouter

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately this did not work properly. Added my code to the post so you can have a look at how I implemented the code you provided me. I still get the error I mention in my post :(
Until useEffect is executed, it is assumed that an error will occur because pageSrc is "". I suggest using if(!pageSrc) return <></>; above render return. It's my shallow knowledge, but I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.