Nope that doesnt work either. Heres my the actual code:
class SessionController extends ControllerBase
{
// Change Main Layout
public function initialize()
{
$this->view->setMainView('plain');
}
// CHECK CSRF TOKEN
public function beforeExecuteRoute(Dispatcher $dispatcher)
{
$action = $dispatcher->getActionName();
$isPost = $this->request->isPost();
if ($isPost && $action == 'login') {
if (!$this->security->checkToken()) {
throw new \Phalcon\Exception('Invalid security token');
}
} elseif ($action == 'logout') {
$key = $this->request->getQuery('key');
$val = $this->request->getQuery('val');
if (!$this->security->checkToken($key, $val)) {
throw new \Phalcon\Exception('Invalid security token', 401);
}
}
return parent::beforeExecuteRoute($dispatcher);
}
public function loginAction()
{
$user = new User();
$form = new \Admin\Forms\LoginForm($user);
if (!$this->webuser->isGuest()) {
throw new \Phalcon\Exception('You are already logged in');
}
if ($this->request->isPost()) {
if ($form->isValid($_POST, $user) && $form->authenticate() ) {
if ($form->login() === true) {
$name = $this->webuser->get('username');
$this->flashSession->success("Welcome {$name}");
return $this->response->redirect('/');
}
}
$this->flashSession->error($form->getAuthMessage());
}
$this->tag->setTitle("Login");
$this->view->setVar('form', $form);
}
class ControllerBase extends \Phalcon\Mvc\Controller
{
/**
* Before every action, check if user is guest, but has remember me cookie token.
* If unable to login user in throw new exception. All backend methods require authorization.
*
* @param \Phalcon\Mvc\Dispatcher $dispatcher
*/
public function beforeExecuteRoute(Dispatcher $dispatcher)
{
$this->registry->csrfKey = $this->security->getTokenKey();
$this->registry->csrfVal = $this->security->getToken();
$controller = $dispatcher->getControllerName();
$action = $dispatcher->getActionName();
// CHECK A USER IS LOGGED IN
if ( ($controller !== 'session' && $action !== 'login') && $this->webuser->isGuest()) {
if ($this->webuser->hasRememberToken()) {
if ($this->webuser->loginUsingRememberToken() ) {
return true;
}
} else {
$this->flashSession->error('You must login');
$this->view->disable();
return $this->response->redirect('/login');
}
throw new \Phalcon\Exception('You are unauthorized to be here', 401);
}
}
}