Next.js Discord

Discord Forum

How can I protect my server actions? How to return status code?

Answered
Common House-Martin posted this in #help-forum
Open in Discord
Avatar
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."


"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: 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 allowed

how 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 enough
View full answer

23 Replies

Avatar
joulev
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
Avatar
Common House-MartinOP
Image
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
Image
this is the actual server action
Avatar
joulev
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
Avatar
Common House-MartinOP
yes, all my use server files exprt an async function
Image
I tried adding async to it but I get errors if I do that
Image
Avatar
Ray
export async function protect() {}
try without default export
Avatar
Common House-MartinOP
nope, that shouldn't matter because it's just a different way of exporting right
Avatar
joulev
hmm i think you'll need to give me a minimal reproduction repository
Avatar
Common House-MartinOP
Avatar
joulev
Thanks, will have a look later
Avatar
joulev
cause: 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 allowed

how 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 enough
Answer
Avatar
Asiatic Lion
@joulev thanks joulev, you just saved me a bunch of time
Avatar
joulev
you're welcome