How to redirect after login based on roles

I’m using an action to try redirect away from an admin page and to a standard user profile if you don’t have an ‘admin’ role. So anyone with a ‘user’ role should be directed to a normal profile page.

But when I try this…

exports.onExecutePostLogin = async (event, api) => {
  if (event.authorization.roles.includes("user")) {
    api.redirect.sendUserTo("http://localhost:3000/user/profile")
    };
  };

My page just says “got redirected too many times” and crashes the app. It’s like an endless redirect loop

How do you implement a redirect like this based on roles?

Hi @nc14,

The redirect method in actions is a temporary redirect, and isn’t meant to be used to finish authentication.

The easiest way to handle this is in your application. If you add the role to the token, you can check the role after login and redirect based on that.

Hope this helps!