Server actions permissions help
Unanswered
Common House-Martin posted this in #help-forum
Common House-MartinOP
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)
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?
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)
import { getSession } from '@/actions/login';
//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?
7 Replies
Common House-MartinOP
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
Common House-MartinOP
@joulev i summon you, ur the goat
Thanks but sorry, I’m busy so am unable to offer help for the foreseeable future
Common House-MartinOP
😢
Common House-MartinOP
@aardani is this possible?
sorry
i tagged you because you answered a similar question