#How can I make private route in app router project?

57 messages · Page 1 of 1 (latest)

mossy shore
#

How can I make private route in app router project?

thorny swallowBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
devout iris
#

you mean adding auth to it?

mossy shore
#

Yes.

#

I am now doing router.push in all components.

#

But if I do like this, when I enter the private route, first it shows that page for milli second and push back.

atomic trout
#

Maybe you can try conditional rendering?

proper warren
#

yes I'd say the same thing

if (!user) return router.push('/login')

return (
// your components
)

but this would probably require loading the user request on server-side, or wait the end of the request to render the pages

mossy shore
#

how can I avoid that?

ruby crypt
#

that way it wont flash

mossy shore
#

Then how about client component?

#

I mean on Client components?

ruby crypt
#

when session is found, the function will return any component that you define in the Page/Layout Component

#

that includes client and server components

mossy shore
#

what do you mean exactly?

ruby crypt
#
const session = getSession()
if(!session) redirect('/login')

return (
  <ClientComponents/>
  <ServerComponents/>
)
#

if session is found it will return any client components or server components

mossy shore
#

I see... But if I do like that I have to use that code in every routes.

#

It's complicated. Can't I make a speicifc component?

ruby crypt
#

not really, just use it on the top level layout.tsx

mossy shore
#

Oh...

ruby crypt
#

that way every code that uses that route segment will need authentication

mossy shore
#

In layout tsx, browser api is working?

#

like session, localstorage

ruby crypt
#

you can put client components in layout.tsx

mossy shore
#

If I put client component, then it is working?

ruby crypt
#

why would i suggest you something that isn't working xd

mossy shore
#

Okay. Thanks. I will try now.

#

where is redirect function?

ruby crypt
#

in next/navigation i believe

mossy shore
#
export default function RootLayout({ children }: { children: React.ReactNode }) {

  const zeroString = localStorage.getItem('zero');
  const zero = zeroString !== null ? JSON.parse(zeroString) : null;

  if (!zero.auth.access_token) {
    redirect('/login')
  }

I did like this but it is saying localStorage is not defined.

ruby crypt
#

i told you you can't use web api in server component
put a client component and use your localStorage in there
put the client component inside a server component

mossy shore
#

Okay.

#

Seems it is not redirecting instantly.

ruby crypt
#

log your session object

mossy shore
#

Ok.

#

It is working in production.

#

Maybe pre building route issue?

ruby crypt
#

delete .next folder maybe? haha

mossy shore
#

You are great.

ruby crypt
#

you can be too!

#

honestly all of these can be answered if you try it on your own nextjs project

#

but im just helping you go to the right direction

mossy shore
#

Ok. Thanks!

#

Just one thing.

{
        zero.auth?.access_token ? <> 
          <Navbar />
          <div className='flex flex-1 overflow-y-auto'>{props.children}</div>
          <Footer />
          <ToastWrapper />
        </> :
        <>
          {props.children}
        </>
      }

Here, if there is zero.auth.access_token it shows those components but not showing all together.
I think it is not good for user experience. Is there any wahy to show them all together?

ruby crypt
#

uhhh wdym not show all together?

#

which comps did not show instantly?

#

navbar? footer? div?

mossy shore
#

Yeah. It is showing one by one I mean.

ruby crypt
#

yes but precisely how?

#

in which order

#

and maybe there is another operation at Navbar, or Footer or Children that i don't know of

#

its not enough information to help you

#

its too vague

mossy shore
#

Okay. Thanks.
I will try to fix.
Thanks for your help! really.
I will open another tag if I need help.

ruby crypt
#

you're welcome anytime