fix(auth): correct login detection + return to origin after login

- isUserLoggedIn() checked the access_token cookie, which is never set
  (access_token lives in memory / tokenStore; only userInfo + uuid are
  cookies). It therefore always returned false — the claim modal (and
  comment auth checks) kept showing the login prompt even when logged in.
  Now reads the userInfo cookie.
- Claim modal login link carries ?redirect=<current path>; after OTP login
  SendReq returns to that path (guarded to internal "/..." only, blocks
  protocol-relative //) instead of always going to "/".

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-11 15:12:58 +03:30
co-authored by Claude Opus 4.8
parent 4fed9a8c57
commit f584f581e5
3 changed files with 11 additions and 5 deletions
+3 -2
View File
@@ -1,7 +1,7 @@
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useRouter, usePathname } from "next/navigation";
import Link from "next/link";
import { Box, Button, Modal, TextField } from "@mui/material";
import { styleDefault } from "@/mui";
@@ -20,6 +20,7 @@ const initialForm = { national_code: "", birth_date: "", first_name: "", last_na
*/
function ClaimProfileSection({ doctor }) {
const router = useRouter();
const pathname = usePathname();
const [open, setOpen] = useState(false);
const [form, setForm] = useState(initialForm);
const [fieldErrors, setFieldErrors] = useState({});
@@ -179,7 +180,7 @@ function ClaimProfileSection({ doctor }) {
برای احراز مالکیت، ابتدا با شماره موبایل خود وارد شوید. پس از ورود،
به همین صفحه بازگردید و دوباره تلاش کنید.
</p>
<Link href="/login" className="w-full">
<Link href={`/login?redirect=${encodeURIComponent(pathname)}`} className="w-full">
<Button variant="contained" className="!rounded-[12px] w-full">
ورود / ثبتنام با موبایل
</Button>
@@ -86,7 +86,12 @@ function SendReq({
if (setStep) {
setStep(3);
} else {
window.location.href = "/";
// بازگشت به صفحهٔ مبدأ اگر ?redirect= داده شده (فقط مسیر داخلی امن)
const redirect = new URLSearchParams(window.location.search).get("redirect");
window.location.href =
redirect && redirect.startsWith("/") && !redirect.startsWith("//")
? redirect
: "/";
}
} else {
setIsError(true);