#Server actions permissions help

9 messages · Page 1 of 1 (latest)

desert flicker
#

I'm trying to make an app with pretty much only server actions, the app doesn't need to interact with the outside, session is stored in cookies with iron-session. I want to have permissions for different roles for those server actions, how and where can I define the middleware for handling this stuff.

I've tought about putting the logic to check the role in the middleware but I don't know how to identify whether a request is a server action or not.

Then I've tought about making a wrapper but when I console logged inside the server action, it displayed on the browser console, probably because it's returning a function and not the result of it but I couldn't get it to work (my previous question was about this but couldn't get a good answer)


//only checks if there's a session
export async function protect(fn: any) {
    return async (...params: any) => {
        if(await getSession()) {
            return await fn(...params);
        } else {
            throw new Error("invalid session")
        }
    }
}```

The easy solution is to make another server action that checks for the role and fn name in each server action but I would have to call it in every single server action and that would be repeating too much code kind of

So how could I handle authorization for my server actions, on the server side so that I can read the session from the cookies and check it against the db before executing them?
rare crownBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

desert flicker
#

I know someone would tell me to just make an api endpoint and call server actions from each endpoint but that's what I'm trying to avoid also

desert flicker
#

@rotund plume i summon you, ur the goat

rotund plume
#

Thanks but sorry, I’m busy so am unable to offer help for the foreseeable future

desert flicker
#

😢

desert flicker
#

@rocky pine is this possible?

lethal sinewBOT
# desert flicker <@194128415954173952> is this possible?
Don't ping or DM other devs you aren't actively talking to

Do not ping other people in order to get attention to your question unless they are actively involved in the discussion. If you're looking to get help, it is a lot better to post your question in a public channel so other people can help or learn from the questions

desert flicker
#

sorry