https://github.com/battis/restful-api.user-session
[READ-ONLY] User session management for Slim Framework
https://github.com/battis/restful-api.user-session
authentication session-management slim-framework user-sessions
Last synced: 8 months ago
JSON representation
[READ-ONLY] User session management for Slim Framework
- Host: GitHub
- URL: https://github.com/battis/restful-api.user-session
- Owner: battis
- License: mit
- Created: 2022-09-05T13:45:07.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-18T19:31:13.000Z (over 2 years ago)
- Last Synced: 2025-01-27T13:04:28.198Z (over 1 year ago)
- Topics: authentication, session-management, slim-framework, user-sessions
- Language: PHP
- Homepage: https://github.com/battis/restful-api/tree/main/packages/user-session
- Size: 189 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Battis\UserSession
[](https://packagist.org/packages/battis/user-session)
[](https://codecov.io/gh/battis/user-session)
User session management for Slim Framework
## Installation
```bash
composer install battis/user-session
```
## Use
See [example](https://github.com/battis/restful-api/tree/main/examples/user-session) for sample implementation. The highlights are:
### Add `UserSession\Dependencies` definitions
Use `UserSession\Dependencies` to prepare container with dependency definitions (this should be done _before_ any additional app-specific definitions wherein you might want to override any of the UserSession defaults):
```php
/** @var DI\ContainerBuilder $containerBuilder */
$containerBuilder->addDefinitions(
Battis\UserSession\Dependencies::definitions()
);
```
### Implement `UserEntityInterface` & `UserRepositoryInterface`
Define implementations of `UserEntityInterface` and `UserRepositoryInterface` and
```php
namespace Example;
class UserEntity implements Battis\UserSession\Entities\UserEntityInterface
{
public function getIdentifier(): string
{
// ...
}
public function passwordVerify(string $password): bool
{
// ...
}
}
```
```php
addDefinitions([
Battis\UserSession\Repositories\UserRepositoryInterface::class => fn() => new Example\UserRepository(),
]);
```
### Define `/auth` endpoints
Use `UserSession\Controller` to define authentication endpoints (`/auth/login` and `/auth/logout`):
```php
/** @var Slim\App $app */
$app->group(
Battis\UserSession\Controller::ENDPOINT,
Battis\UserSession\Controller::class
);
```
### Use `Session` or `RequireAuthentication` middleware
Add a user session that provides access to the currently logged-in user to an endpoint (or group) by adding the `UserSession\Middleware\Session` middleware:
```php
/** @var Slim\App $app */
$app
->get('/home', Example\PageRenderer::class)
->add(Battis\UserSession\Middleware\Session::class);
```
Restrict access to an endpoint (or group) to authenticated users by adding the `UserSession\Middleware\RequireAuthentication` middleware:
```php
/** @var Slim\App $app */
$app
->get('/protected', Example\PageRenderer::class)
->add(Battis\UserSession\Middleware\RequireAuthentication::class);
```