Last active
October 1, 2015 19:48
-
-
Save geggleto/2890cb1fc8a7de987630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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