0
votes

I am developing with CakePHP (2.5.6) and my goal is to use both form and basic auth.

I need this because I call some rest actions from a android app with basic auth. If the user visits the website with a browser I want to use form auth.

I set up my AppController to support both and this works fine. But if I access an action that requires authentication the basic auth alert pops up.

The best way would be to redirect to users -> login. But the basic auth alert pops up first.

How can I get around this? Is there a better solution?

1

1 Answers

1
votes

I solved the problem by my own.

I have added a route prefix for api calls.

Configure::write('Routing.prefixes', array('api'));

in core.php file.

Now i can trigger api calls in the AppController's beforeFilter and add Basic authentication only for the api calls.

public function beforeFilter()
{
   if(isset($this->request->params['api']))
   {
        // api call
       // Pass settings in
       $this->Auth->authenticate = array(
            'Basic' => array('userModel' => 'User')
       );
   }
}

Example action.

PostsController -> index

public function api_index {

}

Url: domain.de/api/posts/index.json