Skip to content

Instantly share code, notes, and snippets.

@sime
Created August 27, 2012 21:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sime/3492706 to your computer and use it in GitHub Desktop.
Save sime/3492706 to your computer and use it in GitHub Desktop.
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