# Redirection of users based on roles

**URL:** <https://community.auth0.com/t/redirection-of-users-based-on-roles/85604>\
**Category:** Get Help\
**Created:** [June 21, 2022, 7:25am UTC](https://community.auth0.com/t/redirection-of-users-based-on-roles/85604 "2022-06-21T07:25:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goossensb.bart](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/goossensb.bart/32/34795_2.png) [@goossensb.bart](https://community.auth0.com/u/goossensb.bart)\
**Post date:** [June 21, 2022, 7:25am UTC](https://community.auth0.com/t/redirection-of-users-based-on-roles/85604/1 "2022-06-21T07:25:31Z")

</div>

I am trying to do this based on actions → flows

```auto
/**
 * @param {Event} event - Details about the user and the context in which they are logging in.
 * @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
 */
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://api';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);

    const token = api.redirect.encodeToken({
    secret: event.secrets.MY_REDIRECT_SECRET,
    expiresInSeconds: 60, 
    payload: {
      // Custom claims to be added to the token
      email: event.user.email,
    },
    });
    if (event.authorization.roles.includes("Admin")) {
      api.redirect.sendUserTo("http://localhost:8910/admin", {
      query: { session_token: token }
    });
    }
  }
}

```

For some odd reason the page now goes straight back to the main page without login in.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [June 24, 2022, 6:51pm UTC](https://community.auth0.com/t/redirection-of-users-based-on-roles/85604/3 "2022-06-24T18:51:10Z")

</div>

Hi @goossensb.bart,

Welcome to the Auth0 Community!

The redirect from Actions is intended for redirect _during authentication_, not after. You must redirect back to an action to complete the flow.

> **[Redirect with Actions](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/redirect-with-actions)**
>
> Learn how to use post-login Actions to redirect users before an authentication transaction is complete.

If you want to redirect _after authentication_, you should do so in your application. You can add the roles to the token and redirect in your application after you get the tokens.

> **[Login Flow](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow#add-user-roles-to-id-and-access-tokens)**
>
> Learn about the Actions Login Flow and the post-login Action trigger, which is executed after a user logs in and when a Refresh Token is requested.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 22, 2022, 9:34am UTC](https://community.auth0.com/t/redirection-of-users-based-on-roles/85604/4 "2022-12-22T09:34:39Z")

</div>


