How can I protect my server actions? How to return status code?
Answered
Common House-Martin posted this in #help-forum
Common House-MartinOP
I need like a middleware that executes the getSession() server action and then if it doesn't return null it executes the next server action but I can't figure out why, I've tried multiple things with adding/removin async, passing the function reference instead of executing it but I always get the same error: "Error: A "use server" file can only export async functions, found object."
Also Is there a way to manage the return status code like for example if I have a server action and I want to make it always return 404 how can I do that soo I can manage the response from the client through .catch()?
"use server";
import protect from "@/lib/protect";
export const display = async () => {
return "ok"
};
export default protect(display)
//@/utils/protect.ts
"use server"
import { getSession } from '@/actions/login';
export default async function protect(fn) {
if(await getSession()) {
return await fn();
} else {
throw new Error("invalid session")
}
}
Also Is there a way to manage the return status code like for example if I have a server action and I want to make it always return 404 how can I do that soo I can manage the response from the client through .catch()?
Answered by joulev
cause:
how to fix: just remove
protect
is not an async function, but a normal function that returns an async function. such a function is still forbidden because, well, only async functions are allowedhow to fix: just remove
use server
from the src/lib/protect.ts
file. protect
is not a server action by itself, just a HOF wrapping server actions, so the use server
in src/actions
files are already enough23 Replies
do you export anything other than async functions in your use server files?
from the error message, it seems you export an object from the file which is not allowed
"use server" file must only export asynchronous functions
Common House-MartinOP
that's the protect file
if I put "use server" it says Error: A "use server" file can only export async functions, found object.
if I remove it it works but why
this is the actual server action
so can you confirm to me that, in all "use server" files in your project, there are only function exports? not something like
export const something = someObject
?the code in the screenshot looks fine to me... but can try
export default async function protect
instead of export default function protect
Common House-MartinOP
yes, all my use server files exprt an async function
I tried adding async to it but I get errors if I do that
export async function protect() {}
try without default export
Common House-MartinOP
nope, that shouldn't matter because it's just a different way of exporting right
hmm i think you'll need to give me a minimal reproduction repository
Common House-MartinOP
Thanks, will have a look later
cause:
how to fix: just remove
protect
is not an async function, but a normal function that returns an async function. such a function is still forbidden because, well, only async functions are allowedhow to fix: just remove
use server
from the src/lib/protect.ts
file. protect
is not a server action by itself, just a HOF wrapping server actions, so the use server
in src/actions
files are already enoughAnswer
Asiatic Lion
@joulev thanks joulev, you just saved me a bunch of time
you're welcome