fix(header): detect login state live from client cookie
The header decided between profile and login button only from the server-passed logged prop (read once via cookies() in StLayout), so a user who logged in client-side still saw ورود | ثبت نام until a hard reload. Seed isLogged from the server prop (correct first paint, no hydration mismatch) then sync it from the access_token cookie on mount and on every route change, so login/logout reflect immediately. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -7,11 +7,19 @@ import Row from "./list/Row";
|
||||
import Col from "./list/col";
|
||||
import ButtonMenu from "./list/col/ButtonMenu";
|
||||
import BgGray from "./list/col/BgGray";
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { usePathname } from "next/navigation";
|
||||
import Cookies from "js-cookie";
|
||||
import ProfileUser from "./profile";
|
||||
|
||||
function Content({ matchedCity, name, logged }) {
|
||||
const [isActive, setIsActive] = useState(false);
|
||||
const [isLogged, setIsLogged] = useState(Boolean(logged));
|
||||
const pathname = usePathname();
|
||||
|
||||
useEffect(() => {
|
||||
setIsLogged(Boolean(Cookies.get("access_token")));
|
||||
}, [pathname]);
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -33,7 +41,7 @@ function Content({ matchedCity, name, logged }) {
|
||||
</div>
|
||||
</div>
|
||||
<Row name={name} />
|
||||
{logged ? (
|
||||
{isLogged ? (
|
||||
<ProfileUser />
|
||||
) : (
|
||||
<Link href="/login">
|
||||
|
||||
Reference in New Issue
Block a user