1

Whenever i logged in i have redirected all to 'welcome.php'. But if i want user to redirect on user page and admin to redirect on admin page. what can I do. ? i have defined types of users already just want to redirect them separately. for eg. user and admin option is there when a new user gets register/signup.

in UsersController.php

 public function login()
{
 $this->viewBuilder()->setLayout('login');
 $this->request->allowMethod(['get', 'post']);
 $result = $this->Authentication->getResult();
 if ($result->isValid()) {
    $redirect = $this->request->getQuery('redirect', [
        'controller' => 'Users',
        'action' => 'welcome',
    ]);
    
    return $this->redirect($redirect);
}
if ($this->request->is('post') && !$result->isValid()) {
    $this->Flash->error(__('Invalid username or password'));
}

}

signup.php

    <label for="utype" class="text-gray-600  mb-2 "> 
      Usertype</label>
    <div class="form-check ">
        <label class="radio-inline">
            <input type="radio" name="utype" value="admin"  
             class=" px-3 py-2  border border-gray-300 rounded-md 
             " />;
         Admin </label>
        <label class="radio-inline">
            <input type="radio" name="utype" value="user"   
             class="px-3 py-2  border border-gray-300 rounded-md"  
             checked/>;
        User </label>
    </div>
 
</div>
  • Does this answer your question? [how do i redirect user to the user page and admin to the admin page in cakephp](https://stackoverflow.com/questions/67317584/how-do-i-redirect-user-to-the-user-page-and-admin-to-the-admin-page-in-cakephp) – Greg Schmidt Apr 30 '21 at 13:37

1 Answers1

0

Are you setting your user type in the controller? if you have an attribute called user type for example, you could check its value and set the "$action" from there

 public function login(){
   $this->viewBuilder()->setLayout('login');
   $this->request->allowMethod(['get', 'post']);
   $result = $this->Authentication->getResult();
   if ($result->isValid()) {
      $action = $this->user_type=='admin'?'admin':'welcome'
      $redirect = $this->request->getQuery('redirect', [
         'controller' => 'Users',
         'action' => $action,
      ]);

   return $this->redirect($redirect);
   }
  if ($this->request->is('post') && !$result->isValid()) {
  $this->Flash->error(__('Invalid username or password'));
}