Fix impersonation: use direct fetch to /api/auth/admin/impersonate-user
authClient.admin.impersonateUser is not registered in pathMethods in better-auth v1.6 client plugin — call the endpoint directly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
|
||||
type Enrollment = {
|
||||
courseId: string;
|
||||
@@ -93,15 +91,21 @@ function UserPopup({ user }: { user: UserRow }) {
|
||||
}
|
||||
|
||||
function ImpersonateButton({ userId }: { userId: string }) {
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function handleImpersonate() {
|
||||
setLoading(true);
|
||||
try {
|
||||
await authClient.admin.impersonateUser({ userId });
|
||||
const res = await fetch("/api/auth/admin/impersonate-user", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ userId }),
|
||||
credentials: "include",
|
||||
});
|
||||
if (!res.ok) throw new Error(await res.text());
|
||||
window.location.href = "/dashboard";
|
||||
} catch {
|
||||
} catch (e) {
|
||||
console.error("Impersonation failed:", e);
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user