BlacksmithProject User System
A PHP User system.
How to use it ?
There are several actions available, from register a new User to reset its password.
Each actions comes with its command, handler and event(s).
How to Register a new User:
You can register a User with an email and a password. The password will be encoded with bcrypt
.
- create a
RegisterUser
command.
$command = new RegisterUser('email@example.com', 'pa$$word');
- handle the command with
RegisterUserHandler
.
$this->registerUserHandler->handle($command);
- [Optional] listen to the event
UserRegisteredEvent
and send an activation email for example.
How to Activate a User:
During registration, a token is generated. Its type is activation
. If the token is expired, a new one will be generated during this process.
- create an
ActivateUser
command.
$command = new ActivateUser('token-value');
- handle the command with
ActivateUserHandler
.
$this->activateUserHandler->handle($command);
- [Optional] listen to the event
UserActivatedEvent
and add a role to User for example. - [Optional] listen to the event
RegeneratedActivationTokenEvent
and send an email with the new token to User.
If you don't want to activate User through email, you can do it on a UserRegisteredEventListener
.
How to Authenticate a User:
Authentication let you check if User exists, is active and that its credentials are valid.
- create an
AuthenticateUser
command.
$command = new AuthenticateUser('email@example.com', 'pa$$word');
- handle the command with
AuthenticateUserHandler
.
$this->authenticateUserHandler->handle($command);
- [Optional] listen to the event
UserAuthenticatedEvent
and start User's session for example.
How to Request a password reset:
Sometimes, User will forget its password, and will need to reset it. It is a two-step action, here is the first part: Request. During the process, a token of type 'reset_password' will be generated.
- create an
RequestResetPassword
command.
$command = new RequestResetPassword('email@example.com');
- handle the command with
RequestResetPasswordHandler
.
$this->requestResetPasswordHandler->handle($command);
- [Optional] listen to the event
ResetPasswordRequestedEvent
and send a reset-password email to User.
How to Reset a password:
Here is the second part of the reset password process: Reset. If the token is expired, a new one will be generated during this process.
- create an
ResetPassword
command.
$command = new ResetPassword('token-value', 'new-password');
- handle the command with
ResetPasswordHandler
.
$this->requestResetPasswordHandler->handle($command);
- [Optional] listen to the event
PasswordResetEvent
and send a password-reset email to User. - [Optional] listen to the event
RegeneratedResetPasswordTokenEvent
and send an email with the new token to User.