Skip to content

Use separately for each page instead of _app #6

@fairhat

Description

@fairhat

Hey,

is it possible to use withLayout for each page?

My setup currently:

export const authenticated = (Component) => {
  return withLayout(<AuthGuard><BaseLayout /></AuthGuard>)(Component)
}

export const public = (Component) => {
  return withLayout(<BaseLayout />)(Component)
}

in each page i then use either authenticated(Component) or public(Component)

AuthGuard:

const AuthCheck = ({ children }) => {
  const [isChecking, setChecking] = React.useState(true)
  const router = useRouter()
  const actions = useActions()
  const state = useState()

  React.useEffect(() => {
    tryAuth()
  }, [])

  React.useEffect(() => {
    if (state.authenticationCheckFailed) {
      redirect()
    }
  }, [state.authenticationCheckFailed])

  React.useEffect(() => {
    if (state.authenticationCheckTried && state.isAuthenticated) {
      setChecking(false)
    }
  }, [state.authenticationCheckTried])

  const redirect = async () => {
    const r = router.pathname.slice(1)
    const redirectTo = r === 'login' ? '/' : '/' + r
    router.push(`/login?r=${redirectTo}`)
    // setChecking(false)
  }

  const tryAuth = async () => {
    await actions.checkAuth(null)
  }

  if (isChecking) {
    return (
      <Centered>
        <LoadingSpinner $style={{ borderTopColor: theme.colors.primary }} />
      </Centered>
    )
  }

  return children
}

export default AuthCheck

however this doesn't seem to work with SSR, it still is rendering my page Component first, then it renders my , even though it should not render any page component until (isChecking) is set to false (which happens if the user was authenticated)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions