Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic Auth in CakePHP on json request
class AppController extends Controller
{
public $components = array(
'RequestHandler',
'Auth',
);
public function beforeFilter() {
$this->__setupAuth();
}
protected function __setupAuth() {
$this->Auth->authenticate = array(
AuthComponent::ALL => array(
'fields' => array(
'username' => 'email',
'password' => 'password'
),
),
'Form',
);
if (isset($this->request->params['ext']) && $this->request->params['ext'] == 'json') {
$this->Auth->authenticate = array('Basic');
if (!$this->Auth->login()) {
$data = array (
'status' => 400,
'message' => $this->Auth->authError,
);
$this->set('data', $data);
$this->set('_serialize', 'data');
$this->viewClass = 'Json';
$this->render();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.