stylers / laravel-email-verification
Model has to verify it's email
0.2.0
2024-01-05 15:03 UTC
Requires
- illuminate/bus: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/database: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/notifications: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- laravel/legacy-factories: ~1.3.2
- mockery/mockery: ^1.1|^1.6
- orchestra/testbench: ^3.0|^4.0|^5.0|^6.0|^7.0|^8.0
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^7.3|^10.0
- themsaid/laravel-mail-preview: ^2.0|^6.0
This package is not auto-updated.
Last update: 2024-11-02 18:56:09 UTC
README
Laravel Email Verification (non-released)
TODO
- Release
- Publish on Packagist
Requirements
- PHP >= 7.1.3
- Laravel ~5.x
Installation
composer require stylers/laravel-email-verification
Publish the assets
php artisan vendor:publish --tag=laravel-mail php artisan vendor:publish --provider="Themsaid\MailPreview\MailPreviewServiceProvider" php artisan vendor:publish --provider="Stylers\EmailVerification\Frameworks\Laravel\ServiceProvider"
Run the migrations
php artisan migrate
Usage
Set up the abstraction
use Stylers\EmailVerification\NotifiableInterface; use Illuminate\Notifications\Notifiable; use Stylers\EmailVerification\EmailVerifiableInterface; use Stylers\EmailVerification\Frameworks\Laravel\Models\Traits\EmailVerifiable; class User extends Model implements NotifiableInterface, EmailVerifiableInterface { use Notifiable; use EmailVerifiable; ... public function getName(): string { return (string)$this->name; } }
Register your listeners to events
// app/Providers/EventServiceProvider.php protected $listen = [ ... 'Stylers\EmailVerification\Frameworks\Laravel\Events\VerificationSuccess' => [ 'your\listener\class1', 'your\listener\class2', ] ]; // OR you can register your listener via Event facade in any ServiceProvider::boot method Event::listen( 'Stylers\EmailVerification\Frameworks\Laravel\Events\VerificationSuccess', 'your\listener\class' );
Example of generating email-verification-request
Make your own route to create verification-request. Write the code below into the routes/web.php and implement your action
Route::post('/email-verification-request', 'AnyController@createEmailVerificationRequest') ->name('email-verification-request.create');
use Stylers\EmailVerification\Frameworks\Laravel\Notifications\EmailVerificationRequestCreate; use Stylers\EmailVerification\Exceptions\AlreadyVerifiedException; ... class AnyController extends Controller { ... public function createRequest( Request $request, EmailVerificationRequestInterface $emailVerificationService ) { $verifiableUser = $notifiableUser = User::first(); try { $verificationRequest = $emailVerificationService->createRequest($verifiableUser->email); $emailVerificationService->sendEmail($verificationRequest->getToken(), $notifiableUser); } catch (AlreadyVerifiedException $e) { // handle exception } ... } }
Example of verification
Make your own verification route. Write the code below into the routes/web.php and implement your action
Route::get('/email/verify/{token}', 'AnyController@verifyEmail') ->name(config('email-verification.route'));
Implement your verifyEmail method in AnyController
... use Stylers\EmailVerification\Exceptions\ExpiredVerificationException; use Stylers\EmailVerification\Exceptions\AlreadyVerifiedException; use Stylers\EmailVerification\EmailVerificationServiceInterface; ... class AnyController extends Controller { ... public function verifyEmail( Request $request, EmailVerificationServiceInterface $emailVerificationService ) { $token = $request->input('token'); try { $emailVerificationRequest = $emailVerificationService->verify($token); } catch(ExpiredVerificationException $e) { // expired verification token } catch(AlreadyVerifiedException $e) { // already verified email } catch(\InvalidArgumentException $e) { // non-existing token } ... // email verification succeed } }