Authenticate with Laminas Authentication using a database callback adapter and session storage.
Authentication with LaminasAuthentication
use LaminasAuthenticationAuthenticationService;
use LaminasAuthenticationAdapterDbTableCallbackCheckAdapter;
use LaminasAuthenticationStorageSession as SessionStorage;
class AuthController extends AbstractActionController {
public function __construct(
private AuthenticationService $auth,
private AdapterInterface $db
) {}
public function loginAction() {
$request = $this->getRequest();
if ($request->isPost()) {
$adapter = new CallbackCheckAdapter(
$this->db, "users", "email", "password",
function($hash, $password) { return password_verify($password, $hash); }
);
$adapter->setIdentity($request->getPost("email"))
->setCredential($request->getPost("password"));
$result = $this->auth->authenticate($adapter);
if ($result->isValid()) {
return $this->redirect()->toRoute("home");
}
// handle failure
}
return new ViewModel();
}
public function logoutAction() {
$this->auth->clearIdentity();
return $this->redirect()->toRoute("login");
}
}
// Check auth
if ($this->auth->hasIdentity()) {
$identity = $this->auth->getIdentity();
}