Created
August 27, 2012 21:58
-
-
Save sime/3492706 to your computer and use it in GitHub Desktop.
Basic Auth in CakePHP on json request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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