#Update parent component

1 messages · Page 1 of 1 (latest)

little halo
#

Right now I have my navigation stored in a component which the root.tsx file uses, when the user has signed in successfully, I want to update the navigation to include the user's name.

My root.tsx file:

function Layout({ children }) {
    return (
        <div className={"wrapper flex-row flex-fill 100vh"}>
            <div className={"d-flex flex-column"}>
                <Dashboard/>
            </div>
            <div className={"d-flex flex-column flex-fill"}>
                {children}
            </div>
        </div>
    );
}
main spire
#

return the currently logged-in user from the loader of root.tsx

little halo
#

Like this?

#
export const loader: LoaderFunction = async ({ request }) => {
    const auth_token = await auth.isAuthenticated(request, {
        failureRedirect: "/login",
    });

    console.log("The index is loading...");
    return null;
}
#

My index route already has the same loader

main spire
#

isAuthenticated should return the user object (including their name). then

return json({ user })

little halo
#

Gotcha, so should I remove this from index and put in root

#

I get too many redirects when I put that loader in root

main spire
#

yeah and in your root component you do
const { user } = useLoaderData<typeof loader>()

#

huh it should only redirect on failure, right?

little halo
#

It keeps redirecting to login over and over

main spire
#

in that case your authentication logic does not seem to work for some reason

little halo
#

it works in index but not root

#

do you have any idea what could cause that behavior? is root doing something different than index?

main spire
#

there shouldn't be any difference between loaders of different routes. auth.isAuthenticated should work the same no matter where

#

sorry, not sure what's causing this in your case 🤷

little halo
#

can you point me in a direction for debug

#

@main spire After getting rid of the failureRedirect it doesn't error infinitely redirect anymore

main spire
#

that's good news. however, it shouldn't do that to begin with 🤔

little halo
#

it's not failing as it's giving me the information I need

#

What actually triggers failureRedirect?

main spire
little halo
#

is it possible it's doing that because session isn't loaded yet?

main spire
#

seems unlikely. what are you using? cookieSession?

little halo
#

Believe so

#

sessionStorage is in my auth.server.tsx

#

I don't have a seperate file called session.server

#

(if that has any effect)

main spire
#

you can check in chrome if the session cookie is set correctly. you can also go into node_modules and console.log the session in the above code to see if it's found

little halo
#

this messes up whenever an error occurs

#
function Layout({ children }) {
    const user_data = useLoaderData();

    return (
        <div className={"wrapper flex-row flex-fill 100vh"}>
            <div className={"d-flex flex-column"}>
                <Dashboard name={user_data.FirstName}/>
            </div>
            <div className={"d-flex flex-column flex-fill"}>
                {children}
            </div>
        </div>
    );
}
#

fixed that part

#

still having the failureRedirect issue

#

@main spire could it be because login route is derived from root?

#

So in order to access login it has to go through root which causes it to keep redirecting?

#

@main spire there's no cookie

little halo
#

Anyone have an idea?

#

I think it has something to do with the loader checking for auth in root.tsx, as even the login page will still try to check for auth