# Ajax request redirect to login page even before UnauthenticatedActions Actions

**URL:** <https://discourse.cakephp.org/t/ajax-request-redirect-to-login-page-even-before-unauthenticatedactions-actions/11251>\
**Category:** Uncategorized\
**Created:** [June 4, 2023, 12:33pm UTC](https://discourse.cakephp.org/t/ajax-request-redirect-to-login-page-even-before-unauthenticatedactions-actions/11251 "2023-06-04T12:33:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daryl](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/daryl/32/2735_2.png) [@daryl](https://discourse.cakephp.org/u/daryl)\
**Post date:** [June 4, 2023, 12:33pm UTC](https://discourse.cakephp.org/t/ajax-request-redirect-to-login-page-even-before-unauthenticatedactions-actions/11251/1 "2023-06-04T12:33:07Z")

</div>

Hi Developers I need help, I am using RBAC plugin and I set some permission to user which are not login or Not Authorized they can register and this system work fine until ajax request. I am using Ajax plugin for handle the ajax request now problem is that when i register with ajax request i got in response LOGIN page content, Not show register method content… this is my problem anyone help me why it happen, Is that bug in RBAC plugin with redirect ajax request to login method.

Please help me guys

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [June 4, 2023, 7:31pm UTC](https://discourse.cakephp.org/t/ajax-request-redirect-to-login-page-even-before-unauthenticatedactions-actions/11251/2 "2023-06-04T19:31:00Z")

</div>

What you do in your `register` method (which usually does the same as the `add()` method) is up to you and your controller.

So check your UsersController methods and what you do when a register has been processed. I guess there will be a `$this->redirect()` in there.

---

<div class="post-metadata">

**Author:** ![daryl](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/daryl/32/2735_2.png) [@daryl](https://discourse.cakephp.org/u/daryl)\
**Post date:** [June 5, 2023, 4:15am UTC](https://discourse.cakephp.org/t/ajax-request-redirect-to-login-page-even-before-unauthenticatedactions-actions/11251/3 "2023-06-05T04:15:13Z")

</div>

no thiere is no any redirect method let me show you my code:-

//userscontroller  
$this-\>Authentication-\>addUnauthenticatedActions([‘login’,‘add’,‘delete’,‘index’]);  
if(in\_array($this-\>request-\>getParam(‘action’),[‘add’,‘login’,‘logout’,‘index’])):  
$this-\>Authorization-\>skipAuthorization();  
endif;

public function login(){  
//login logic here  
}

//ajax plugin for handle ajax request

public method add(){  
$user = $this-\>Users-\>newEmptyEntity();  
if ($this-\>request-\>is(‘post’)) {

```
        $user = $this->Users->patchEntity($user, $this->request->getData(),['associated'=>['Profiles']]);
        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        if(!$user->hasErrors()):
        $this->Flash->error(__('The user could not be saved. Please, try again.'));
        endif;

    }
    $this->set(compact('user'));

```

}

//permission file in config/permission.php  
use CakeDC\Auth\Rbac\Rules\Owner;  
return [  
‘CakeDC/Auth.permissions’=\>[  
[  
‘role’=\>‘\*’,  
‘controller’=\>‘Users’,  
‘action’=\>[‘add’,‘login’,‘logout’],  
‘bypassAuth’=\>true  
],  
]  
]

//js file

axios.post(‘users.json’).then(response=\>{

```
              /// in response object from cakephp
            // {content:'login page',error:null,success:'',_message:''}
          // I got login page here not getting add method response

```

})

---

<div class="post-metadata">

**Author:** ![Kaiser85](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kaiser85/32/2714_2.png) [@Kaiser85](https://discourse.cakephp.org/u/Kaiser85)\
**Post date:** [June 5, 2023, 6:32pm UTC](https://discourse.cakephp.org/t/ajax-request-redirect-to-login-page-even-before-unauthenticatedactions-actions/11251/4 "2023-06-05T18:32:15Z")

</div>

Shouldn’t there be a url in your post request?

```auto
axios.post('users.json').then(response=>{
    /// in response object from cakephp
    // {content:'login page',error:null,success:'',_message:''}
    // I got login page here not getting add method response
})

```

---

<div class="post-metadata">

**Author:** ![daryl](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/daryl/32/2735_2.png) [@daryl](https://discourse.cakephp.org/u/daryl)\
**Post date:** [June 7, 2023, 7:53am UTC](https://discourse.cakephp.org/t/ajax-request-redirect-to-login-page-even-before-unauthenticatedactions-actions/11251/5 "2023-06-07T07:53:12Z")

</div>

no its not ajax issue… and user.json is a url and I got response from server(cakephp)… but i got login direct page html content.  
I think cakephp RBAC plugin have problem because if i send normal request from browser then it work fine but if i do same thing with ajax request then permission not allowed and redirect to the login page. But its ok I handle it with my own code.
