Next.js Discord

Discord Forum

Issue with "use server" in form action on Next.js

Answered
Patagonian Sheepdog posted this in #help-forum
Open in Discord
Patagonian SheepdogOP
Hey everyone,

I'm new to all the nextjs stuff and i'm trying to use a Server Action with "use server" inside the action attribute of a <form> in Next.js like it's specified in the doc, but I'm running into an error. Here’s my code:

<form action={async () => {
"use server";
await signIn("github");
}}>
<button type="submit">Login</button>
</form>

I tried to tried to create the function outside the return:
async function loginWithGitHub() {
'use server'
await signIn("github");
}

async function logoutWithGitHub() {
'use server'
await signOut();
}

and then put the function inside the form action{loginWithGitHub}

The error i run into appears after clicking at the login button (never tried logout) :

[ Server ] Error: Attempted to call signIn() from the server but signIn is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.
Answered by Patagonian Sheepdog
I found how to do it, you have to create another file where you create the login and logout fonction, on this file you put 'use server' at the top. Then you can import the fonctions you created in your main file and use the fonction inside form action
View full answer

1 Reply

Patagonian SheepdogOP
I found how to do it, you have to create another file where you create the login and logout fonction, on this file you put 'use server' at the top. Then you can import the fonctions you created in your main file and use the fonction inside form action
Answer