# User redirect after login based on role

**URL:** <https://discourse.cakephp.org/t/user-redirect-after-login-based-on-role/7376>\
**Category:** Need Help\
**Created:** [March 2, 2020, 2:04pm UTC](https://discourse.cakephp.org/t/user-redirect-after-login-based-on-role/7376 "2020-03-02T14:04:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [March 2, 2020, 2:04pm UTC](https://discourse.cakephp.org/t/user-redirect-after-login-based-on-role/7376/1 "2020-03-02T14:04:23Z")

</div>

Hello,

how do I get the user role to pass to the controller to set the redirect to the individual section of the site??

```
public function login() {

    $this->request->allowMethod(['get', 'post']);

    $result = $this->Authentication->getResult();

    

    // regardless of POST or GET, redirect if user is logged in

    if ($result->isValid()) {

        // redirect to /articles after login success

        if ($role == 1) :

        $redirect = $this->request->getQuery('redirect', [

            'controller' => 'Admin',

            'action' => 'index',

        ]);

         elseif ($role == 2) :

            $redirect = $this->request->getQuery('redirect', [

                'controller' => 'Dashboard',

                'action' => 'index',

            ]);

            endif;

        return $this->redirect($redirect);

    }

    // display error if user submitted and authentication failed

    if ($this->request->is('post') && !$result->isValid()) {

        $this->Flash->error(__('Invalid username or password'));

    }

}

```

Regards

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 2, 2020, 4:39pm UTC](https://discourse.cakephp.org/t/user-redirect-after-login-based-on-role/7376/2 "2020-03-02T16:39:13Z")

</div>

You [get the identity object](https://book.cakephp.org/authentication/2/en/identity-object.html) and look up the role in that.

---

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [March 2, 2020, 7:06pm UTC](https://discourse.cakephp.org/t/user-redirect-after-login-based-on-role/7376/3 "2020-03-02T19:06:15Z")

</div>

@Zuluru

This is completely doing my head in! after reading the link, my take on it is as follows:

`App>src>Model Entity/User.php` add the following:

`use Authentication\IdentityInterface;`

```
class User extends Entity implements IdentityInterface
{
        /**
         * Authentication\IdentityInterface method
         */
        public function getIdentifier()
        {
            return $this->role; // field in the user table                
        }

```

In App\>src\>Controller\>UserController.php

```
public function login()
{
    $authentication = $this->request->getAttribute('authentication');
    $result = $authentication->getResult(); 
    if ($result->isValid()) {
        if ($authentication->identifiers()->get('role')) {
            $role = (1); #not sure if it's called this way
            $redirect = $this->request->getQuery('redirect', [
             'controller' => 'Dashboard',
                'action' => 'index',
            ]);
        }

        if ($authentication->identifiers()->get('role')) {

            $user->role = (2); #not sure if it's called this way either??
            $redirect = $this->request->getQuery('redirect', [
             'controller' => 'Partners',
                'action' => 'index',

            ]);

        }

        return $this->redirect($redirect);
    }
      // display error if user submitted and authentication failed
      if ($this->request->is('post') && !$result->isValid()) {
        $this->Flash->error(__('Invalid username or password'));
    }
}

```

not entirely sure how to write that bit, which ever way I try to write it, I get the error of Unknown object “role”

Regards

Mal
