Is it currently possible to use clerk auth with NextJS' 13.4 server actions?
I know it's currently in alpha, but I'd like to know if there is any way to use it right now.
Library versions are the latest.
Library versions are the latest.
@acme/nextjs:dev: - error Error: Clerk: auth() and currentUser() are only supported in App Router (/app directory).
@acme/nextjs:dev: If you're using /pages, try getAuth() instead.
@acme/nextjs:dev: Original error: Error: Invariant: Method expects to have requestAsyncStorage, none available
@acme/nextjs:dev: at handleUpdateNoteContents (./src/app/@modal/(.)e/[id]/actions.ts:13:82)@acme/nextjs:dev: - error Error: Clerk: auth() and currentUser() are only supported in App Router (/app directory).
@acme/nextjs:dev: If you're using /pages, try getAuth() instead.
@acme/nextjs:dev: Original error: Error: Invariant: Method expects to have requestAsyncStorage, none available
@acme/nextjs:dev: at handleUpdateNoteContents (./src/app/@modal/(.)e/[id]/actions.ts:13:82)// actions.ts
"use server";
import { currentUser } from "@clerk/nextjs";
import { db } from "@acme/db";
export const handleUpdateNoteContents = async (id: string, content: string) => {
const user = await currentUser();
if (!user) {
return {
error: "You must be logged in to update a note",
};
}
const { id: userId } = user;
await db
.updateTable("Note")
.set({
content,
})
.where(({ and, cmpr }) =>
and([cmpr("id", "=", id), cmpr("userId", "=", userId)]),
)
.executeTakeFirstOrThrow();
};// actions.ts
"use server";
import { currentUser } from "@clerk/nextjs";
import { db } from "@acme/db";
export const handleUpdateNoteContents = async (id: string, content: string) => {
const user = await currentUser();
if (!user) {
return {
error: "You must be logged in to update a note",
};
}
const { id: userId } = user;
await db
.updateTable("Note")
.set({
content,
})
.where(({ and, cmpr }) =>
and([cmpr("id", "=", id), cmpr("userId", "=", userId)]),
)
.executeTakeFirstOrThrow();
};// page.tsx
'use client'
// ...
export default function EditNoteModal({ params }: { params: { id?: string } }) {
const [isPending, startTransition] = useTransition();
// ...
const router = useRouter();
// ..
const updateContent = ({ content }: EditFormData) => {
const add = async () =>
startTransition(() => {
handleUpdateNoteContents(params.id!, content);
});
reset();
add()
.then(() => {
router.refresh();
router.back();
})
.catch((err) => console.error(err));
};
//...
}// page.tsx
'use client'
// ...
export default function EditNoteModal({ params }: { params: { id?: string } }) {
const [isPending, startTransition] = useTransition();
// ...
const router = useRouter();
// ..
const updateContent = ({ content }: EditFormData) => {
const add = async () =>
startTransition(() => {
handleUpdateNoteContents(params.id!, content);
});
reset();
add()
.then(() => {
router.refresh();
router.back();
})
.catch((err) => console.error(err));
};
//...
}