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
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

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
@Common House-Martin Click to see attachment
export async function protect() {}
try without default export
@Ray `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
@Common House-Martin yes, all my use server files exprt an async function
hmm i think you'll need to give me a minimal reproduction repository
Thanks, will have a look later
@Common House-Martin here https://github.com/ericd33/broken
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
Asiatic Lion
@joulev thanks joulev, you just saved me a bunch of time
you're welcome