yaquawa / laravel-email-reset
Laravel package for email reset.
Installs: 2 608
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.1.3
- illuminate/auth: ^6.0 || ^7.0
- illuminate/http: ^6.0 || ^7.0
- illuminate/notifications: ^6.0 || ^7.0
- illuminate/support: ^6.0 || ^7.0
- yaquawa/laravel-token-repository: 1.*
README
A Laravel package for resetting user email by sending a verification link to the new email.
This package reset the user's email by:
- Send verification link to user's new email.
- If the user clicks the verification link in their new email, the package will verify the new email and set the old email to the new one.
How to install
To get started, follow the following steps.
Installation
composer require yaquawa/laravel-email-reset
Configuration
Add the following code to your <config/auth.php>
file.
'defaults' => [ 'guard' => 'web', 'passwords' => 'users', 'email-reset' => 'default' // Add this line ], // Add this entire block 'email-reset' => [ 'default' => [ 'table' => 'email_resets', 'expire' => 60, 'callback' => 'App\Http\Controllers\Auth\ResetEmailController@reset', // 'ignore-migrations' => true, // 'route' => 'email/reset/{token}', ] ]
Migration
php artisan migrate
If you would like to use your own migration, set ignore-migrations
to true
in the config file (See the migration file here).
Publish the assets
The following command publishes the package's controller and translation files to your app's directories.
php artisan vendor:publish --tag=laravel-email-reset
Use the CanResetEmail
trait
In your app/Models/User.php
file, use the CanResetEmail
trait. This trait adds a resetEmail
method to the user model.
namespace App\Models; use Yaquawa\Laravel\EmailReset\CanResetEmail; class User extends Authenticatable { use CanResetEmail; }
Usage
Send the verification email
// By calling the `resetEmail` method of `User` instance, // an verification email will be sent to the user's new email address. // If the user clicked the verification link, the new email address will be set. // * The route of the verification link can be set at `route` in the config file. $user->resetEmail('new_email@example.com');
If you want to change the email contents, you can do something like this in your AppServiceProvider.php
.
namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Notifications\Messages\MailMessage; use Yaquawa\Laravel\EmailReset\Notifications\EmailResetNotification; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { EmailResetNotification::toMailUsing(function ($user, $token, $resetLink) { return (new MailMessage) ->line('You are receiving this email because we received a email reset request for your account.') ->action('Reset Email', $resetLink) ->line('If you did not request a email reset, no further action is required.'); }); } }
After the user clicked the verification link, by default, the user will be redirected to the root of your app URL.
You can change this behavior by overriding the methods of the published controller ResetEmailController
.
namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Yaquawa\Laravel\EmailReset\ResetEmail; class ResetEmailController extends Controller { use ResetEmail; /** * This method will be called if the token is invalid. * Should return a response that representing the token is invalid. * * @param $status * * @return mixed */ protected function sendResetFailedResponse(string $status) { return redirect($this->redirectPathForFailure())->withErrors(['laravel-email-reset' => trans($status)]); } /** * This method will be called if the token is valid. * New email will be set for the user. * Should return a response that representing the email reset has succeeded. * * @param $status * * @return mixed */ protected function sendResetResponse(string $status) { return redirect($this->redirectPathForSuccess())->with('laravel-email-reset', trans($status)); } /** * The redirect path for failure. * * @return string */ protected function redirectPathForFailure(): string { return '/'; } /** * The redirect path for success. * * @return string */ protected function redirectPathForSuccess(): string { return '/'; } }
Retrieve the "new email"
The new email won't be saved until the user clicks the verification link. Before user clicking the verification link you can get the new email by:
$user->new_email; // retrieve the `new_email` from database