Check this example: https://github.com/phalcon/invo/blob/master/app/controllers/SessionController.php
This is a very minimalist implementation of what you want:
users.txt
foo;hashedPassword
bar;hashedPassword
Auth controller:
class SessionController extends ControllerBase
{
private function _lookupUser($user, $password)
{
$found = false;
$fh = fopen('users.txt', 'r');
flock($fh, LOCK_SH);
while($row = fgetcsv($fh, 512, ';')) {
if($row[0] == $user && $row[1] == $this->security->hash($password)) {
$found = true;
break;
}
}
flock($fh, LOCK_UN);
fclose($fh);
return $found;
}
private function _registerSession(array $user)
{
$this->session->set('auth', $user);
}
public function startAction()
{
if ($this->request->isPost()) {
$user = $this->request->getPost('user');
$password = $this->request->getPost('password');
if(!$this->_lookupUser($user, $password)) {
$this->_registerSession(array('user'=>$user));
$this->flash->success('Welcome ' . $user);
return $this->dispatcher->forward(
[
"controller" => "invoices",
"action" => "index",
]
);
}
$this->flash->error('Wrong user/password');
}
return $this->dispatcher->forward(
[
"controller" => "session",
"action" => "index",
]
);
}
}
Then in any controller, just access $this->session->get("user")