Skip to content

Instantly share code, notes, and snippets.

@simkimsia
Created December 4, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simkimsia/43b048e9bbb95dba08b3 to your computer and use it in GitHub Desktop.
Save simkimsia/43b048e9bbb95dba08b3 to your computer and use it in GitHub Desktop.
<?php
namespace App\Exception;
use Cake\Core\Exception\Exception;
class InvalidAuthenticationException extends Exception
{};
<?php
namespace App\Controller\Api;
use App\Controller\AppController;
use Cake\Event\Event;
use App\Exception\InvalidAuthenticationException;
use Cake\Core\Exception\UnauthorizedException;
/**
* Sessions Controller
*
* @property \App\Model\Table\SessionsTable $Sessions
*/
class SessionsController extends AppController
{
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
}
/**
* Session add method representing user authentication
*
* @return void Redirects on successful , renders view otherwise.
*/
public function add()
{
if ($this->request->is('get')) {
$user = $this->Auth->user();
if ($user) {
$this->set('data', $user);
}
}
// requires username/email, password,
// and preferably redirect
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->set('data', $user);
$this->set('_serialize', ['data']);
return;
}
$this->set('error',[
'type' => 'Invalid authentication',
'code' => 'INVALID_AUTH',
'http_code' => 422,
'message' => 'Invalid email or password, try again',
]);
$this->set('_serialize', ['error']);
throw new InvalidAuthenticationException('asd', 422);
}
}
public function delete()
{
// priority to $_REQUEST['redirect']
$explicitRedirectAfterLogout = $_REQUEST['redirect'];
// followed by app default redirect
$defaultRedirect = $this->Auth->logout();
$redirect = empty($explicitRedirectAfterLogout) ? $defaultRedirect : $explicitRedirectAfterLogout;
return $this->redirect($redirect);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment