Updating Navbar on changed auth state
Unanswered
Swedish Lapphund posted this in #help-forum
Swedish LapphundOP
I have a navbar that shows options like "Log in" and "Create account" when the user isn't authenticated. The navbar is in the root layout and follows the app eveywhere. How can I make it so that it's updated when the user signs in?
Becasue right now it still shows those un-authenticated options after the user has signed in. If the user refreshes the page then it shows the new options, that should be shown
Becasue right now it still shows those un-authenticated options after the user has signed in. If the user refreshes the page then it shows the new options, that should be shown
11 Replies
Swedish LapphundOP
Below is the Navbar component:
async function getProfileInfo() { // this returns "User not authenticated" if user is not logged in. Otherwise it returns the profile info
const cookieStore = cookies();
let headers = {};
const userCookie = cookieStore.get('user_sid');
if (userCookie) {
headers['Cookie'] = `${userCookie.name}=${userCookie.value}`;
}
try {
const response = await fetch(`${apiUrl}/profile/getinfo`, {
method: 'GET',
headers: headers
});
const json = await response.json();
console.log(json)
return json;
} catch (error) {
console.log(error.message);
}
}
async function returnUserContent(profileInfo) { // adds either avatar or "sign in/sign up" options to the navbar depending on auth state
console.log(profileInfo)
if(profileInfo.message === "User not authenticated") {
return(
<>
<Link href="/login" className={buttonVariants({ variant: "outline" })}><LogIn className="mr-2 h-4 w-4" />Log in</Link>
<Link href="/login" className={buttonVariants({ variant: "outline" })}><UserPlus className="mr-2 h-4 w-4" />Create account</Link>
</>
)
} else if(profileInfo.status === "success" && profileInfo.is_verified) {
return(
<Avatar>
<AvatarFallback>TN</AvatarFallback>
</Avatar>
);
} else if(profileInfo.status === "success" && !profileInfo.is_verified) {
return(
<>
<Link href="/login" className={buttonVariants({ variant: "outline" })}><LogIn className="mr-2 h-4 w-4" />Get verified</Link>
<Avatar>
<AvatarFallback>TN</AvatarFallback>
</Avatar>
</>
);
}
}
export async function MainNav() {
const profileInfo = await getProfileInfo()
return ( // stripped of other components
{await returnUserContent(profileInfo)}
);
}Im thinking of NOT rendering the navbar on the log in and sign up screens, then this wont be an issue.. But it would be interesting to know how to do this. Im pretty new
Swedish LapphundOP
@American black bear
When you click on login, it redirects to login page, then what happens?
@American black bear When you click on login, it redirects to login page, then what happens?
Swedish LapphundOP
Then on the log in page - when the user is successfully logged in, it redirects to
It makes sense to me that the values are NOT updated on auth change, but wondering what the way to proceeed if if i want it to change
/profileIt makes sense to me that the values are NOT updated on auth change, but wondering what the way to proceeed if if i want it to change
American black bear
I'm not sure, but it's probably because of caching inside Navbar Component, you can revalidate it after logging in
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation.
I'm not sure, I'm new to the new cache system too.
Anyway, I am probably going outside for 2 hrs, so I will not be available, all the best ðŸ‘
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation.
I'm not sure, I'm new to the new cache system too.
Anyway, I am probably going outside for 2 hrs, so I will not be available, all the best ðŸ‘
Swedish LapphundOP
Big thanks for your input, really appreaciate it so much 🙂
American black bear
NP, lemme know here if it works
American black bear
@Swedish Lapphund any updates? just asking
@American black bear <@346009838674771970> any updates? just asking
Swedish LapphundOP
I didn't fully understand the docs to be honest and haven't continued experimenting, will update you here