Skip to main content

Nextjs Server Actions ⭐️

The nextjs 14 officially release the server actions found at https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations.

Most of server side(app/actions/*) processing like payments, subscriptions, authentication and others uses server action to perform the database processing and third party api call.

Server actions implementation

Checkout some of the server implementation like the login

Navigate to your directory: /app/actions/login.ts: app/actions/login.ts:

app/actions/login.ts
"use server";

import { signIn } from "../../auth";

/**
* This action will signin user.
*/

interface LoginProps {
provider: string;
email?: any;
}
export async function login({ provider, email }: LoginProps) {
try {
if (provider === "google") {
await signIn("google");
}

// email reference:https://authjs.dev/getting-started/authentication/email
if (provider === "email") {
return await signIn("resend", { email, redirect: false }); // you can modify this base on your email provider
}
} catch (error: any) {
throw error;
}
}