Skip to content

Instantly share code, notes, and snippets.

@geggleto
Last active October 1, 2015 19:48
Show Gist options
  • Select an option

  • Save geggleto/2890cb1fc8a7de987630 to your computer and use it in GitHub Desktop.

Select an option

Save geggleto/2890cb1fc8a7de987630 to your computer and use it in GitHub Desktop.
/*
Building a Middleware for Authentication
Components:
1) AuthFactory
- Responsible for creating the the session and loading it with data
*/
interface IAuthFactory {
public function setData($data = []);
}
/*
2) Authenticator
- Responsbile for checking to see if its a valid user
*/
interface IAuthenticator {
public function authorize($username, $password);
}
/*
3) AuthMiddleware
- Responsible for checking to make sure the user is authenticated
*/
interface IAuthMiddleware {
public function __invoke(Request $req, Response $resp , $next);
}
//Usuage...
$securityMiddleware = new AuthMiddleware();
$app->get('/protect', 'Callable)->addMiddleware($securityMiddleware);
$app->post('/login', function ($req, $res, $args) {
$result = $this->authenticator($req->getParsedBody()['username'], $req->getParsedBody()['password']);
if ($result) {
$this->authFactory->setData([]);
//..
} else {
//..
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment