Docs

You are viewing an archived version of the docs.Go to latest version

Server Actions

Clerk provides helpers to allow you to protect your Server Actions, fetch the current user, and interact with the Clerk API.

Below are some examples of usage both in Server Components or Client Components.

With server components

Protect your actions

actions.[js/ts]
import { auth } from '@clerk/nextjs';

export default function AddToCart() {
  async function addItem(formData: FormData) {
    'use server';
    const { userId } = auth();
    if (!userId) {
      throw new Error('You must be signed in to add an item to your cart');
    }
    console.log('add item server action', formData);
  }

  return (
    <form action={addItem}>
      <input
        value={'test'}
        type='text'
        name='name'
      />
      <button type='submit'>Add to Cart</button>
    </form>
  );
}

Accessing the current user

Current user data is important for data enrichment. You can use the currentUser() helper to achieve this.

app/page.tsx
import { currentUser } from '@clerk/nextjs';

export default function AddHobby() {
  async function addHobby(formData: FormData) {
    'use server';
    const user = await currentUser();

    if (!user) {
      throw new Error('You must be signed in to use this feature');
    }

    const serverData = {
      usersHobby: formData.get("hobby"),
      userId: user.id,
      profileImage: user.profileImageUrl
    };

    console.log('add item server action completed with user details ', serverData);
  }

  return (
    <form action={addHobby}>
      <input
        value={'soccer'}
        type='text'
        name='hobby'
      />
      <button type='submit'>Submit your hobby</button>
    </form>
  );
}

With Client Components

When using Client Components, you need to make sure you use prop drilling to ensure that headers are available.

Protect your actions

Server Action code

actions.[js/ts]
'use server'
import { auth } from '@clerk/nextjs';

export async function addItem(formData: FormData) {
  const { userId } = auth();

  if (!userId) {
    throw new Error('You must be signed in to add an item to your cart');
  }

  console.log('add item server action', formData);
}
page.tsx
import UI from "./UI";
import { addItem } from "./actions"
export default function Hobby() {
  return (
    <UI addItem={addItem} />
  );
}
ui.tsx
"use client"

export default function UI({ addItem }) {
  return (
    <form action={addItem}>
      <input
        value={'test'}
        type='text'
        name='name'
      />
      <button type='submit'>Add to Cart</button>
    </form>
  )
}
actions.[js/ts]
'use server'
import { currentUser } from "@clerk/nextjs";

export async function addHobby(formData: FormData) {
  const user = await currentUser();

  if (!user) {
    throw new Error('You must be signed in to use this feature');
  }

  const serverData = {
    usersHobby: formData.get("hobby"),
    userId: user.id,
    profileImage: user.profileImageUrl
  };

  console.log('add Hobby completed with user details ', serverData);
}
page.tsx
import UI from "./ui";
import { addHobby } from "./actions"
export default function Hobby() {
  return (
    <UI addHobby={addHobby} />
  );
}
ui.tsx
"use client"

export default function UI({ addHobby }) {

  return (
    <form action={addHobby}>
      <input
        value={'soccer'}
        type='text'
        name='hobby'
      />
      <button type='submit'>Submit your hobby</button>
    </form>
  )
}

Feedback

What did you think of this content?

Last updated on