Don't force password change while admin is impersonating
The mustChangePassword guard redirected an impersonating admin to /change-password, which sits outside the student layout (no return-to-admin banner) — a dead end. Skip the guard when impersonating, and add the stop-impersonate banner to the change-password page as a fallback. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -11,12 +11,16 @@ export default async function StudentLayout({ children }: { children: React.Reac
|
|||||||
const session = await auth.api.getSession({ headers: await headers() });
|
const session = await auth.api.getSession({ headers: await headers() });
|
||||||
if (!session) redirect("/login");
|
if (!session) redirect("/login");
|
||||||
|
|
||||||
// Force password change for auto-provisioned buyers (temp password)
|
const isImpersonating = !!(session.session as { impersonatedBy?: string }).impersonatedBy;
|
||||||
|
|
||||||
|
// Force password change for auto-provisioned buyers (temp password).
|
||||||
|
// Skip while an admin is impersonating — they only view the client's cabinet;
|
||||||
|
// the client sets their own password on a real first login.
|
||||||
const dbUser = await prisma.user.findUnique({
|
const dbUser = await prisma.user.findUnique({
|
||||||
where: { id: session.user.id },
|
where: { id: session.user.id },
|
||||||
select: { mustChangePassword: true },
|
select: { mustChangePassword: true },
|
||||||
});
|
});
|
||||||
if (dbUser?.mustChangePassword) redirect("/change-password");
|
if (dbUser?.mustChangePassword && !isImpersonating) redirect("/change-password");
|
||||||
|
|
||||||
// Maintenance mode: non-admin users see the maintenance page
|
// Maintenance mode: non-admin users see the maintenance page
|
||||||
if (session.user.role !== "admin") {
|
if (session.user.role !== "admin") {
|
||||||
@@ -35,8 +39,6 @@ export default async function StudentLayout({ children }: { children: React.Reac
|
|||||||
getSetting("orgRequisites"),
|
getSetting("orgRequisites"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const isImpersonating = !!(session.session as { impersonatedBy?: string }).impersonatedBy;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen flex flex-col" style={{ backgroundColor: "var(--background)" }}>
|
<div className="min-h-screen flex flex-col" style={{ backgroundColor: "var(--background)" }}>
|
||||||
{isImpersonating && <StopImpersonateBanner userName={session.user.name} />}
|
{isImpersonating && <StopImpersonateBanner userName={session.user.name} />}
|
||||||
|
|||||||
@@ -2,16 +2,21 @@ import { headers } from "next/headers";
|
|||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { ForceChangeForm } from "./force-change-form";
|
import { ForceChangeForm } from "./force-change-form";
|
||||||
|
import { StopImpersonateBanner } from "@/components/admin/stop-impersonate-banner";
|
||||||
|
|
||||||
export default async function ChangePasswordPage() {
|
export default async function ChangePasswordPage() {
|
||||||
const session = await auth.api.getSession({ headers: await headers() });
|
const session = await auth.api.getSession({ headers: await headers() });
|
||||||
if (!session) redirect("/login");
|
if (!session) redirect("/login");
|
||||||
|
|
||||||
|
const isImpersonating = !!(session.session as { impersonatedBy?: string }).impersonatedBy;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="min-h-screen flex items-center justify-center p-4"
|
className="min-h-screen flex flex-col"
|
||||||
style={{ backgroundColor: "var(--background)" }}
|
style={{ backgroundColor: "var(--background)" }}
|
||||||
>
|
>
|
||||||
|
{isImpersonating && <StopImpersonateBanner userName={session.user.name} />}
|
||||||
|
<div className="flex-1 flex items-center justify-center p-4">
|
||||||
<div className="w-full max-w-sm">
|
<div className="w-full max-w-sm">
|
||||||
<h1 className="text-xl font-bold mb-2" style={{ color: "var(--foreground)" }}>
|
<h1 className="text-xl font-bold mb-2" style={{ color: "var(--foreground)" }}>
|
||||||
Задайте свой пароль
|
Задайте свой пароль
|
||||||
@@ -22,5 +27,6 @@ export default async function ChangePasswordPage() {
|
|||||||
<ForceChangeForm />
|
<ForceChangeForm />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user