kaoken/laravel-email-reset
| Install | |
|---|---|
composer require kaoken/laravel-email-reset |
|
| Latest Version: | 1.0.3 |
| PHP: | >=7.0.0 |
| Laravel: | 5 |
| License: | MIT |
| Last Updated: | Jun 23, 2019 |
| Links: | GitHub · Packagist |
laravel-email-reset
Request to change the e-mail address of the Auth user, change it after moving to the specified URL of the confirmation e-mail.
Table of content
Install
composer:
composer require kaoken/laravel-email-reset
Setting
Add to config\app.php as follows:
'providers' => [
...
// add
Kaoken\LaravelMailReset\MailResetServiceProvider::class
],
'aliases' => [
...
// add
'MailReset' => Kaoken\LaravelMailReset\Facades\MailReset::class
],
Example of adding to config\auth.php
add 'email_reset' => 'users',.
[
...
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
// add
'email_reset' => 'users',
],
...
]
modelis a user model classemail_confirmationshould modify the class derived fromMailable as necessary. Used to send confirmation mail.tableis the name of the table used for this service- If
expiredoes not manipulate X hours after registration, the 1st change email is deleted.
'email_resets' => [
'users' => [
'model' => App\User::class,
'email_reset' => Kaoken\LaravelMailReset\Mail\MailResetConfirmationToUser::class,
'table' => 'mail_reset_users',
'expire' => 1,
]
],
Command
php artisan vendor:publish --tag=mail-reset
After execution, the following directories and files are added.
databasemigrations2017_09_21_000001_create_mail_reset_users_table.php
resourceslangenmail_reset.php
jamail_reset.php
viewsvendorconfirmationmailconfirmation.blade.php
complete.blade.blade.php
Migration
Migration file 2017_09_21_000001_create_mail_reset_users_table.php should be modified as necessary.
php artisan migrate
Add to kernel
Add it to the schedule method of app\Console\Kernel.php.
This is used to delete users who passed 1 hour after 1st registration.
protected function schedule(Schedule $schedule)
{
...
$schedule->call(function(){
MailReset::broker('users')->deleteUserAndToken();
)->hourly();
}
In the configuration config\auth.php with the above setting,
Kaoken\LaravelMailReset\Mail\MailResetConfirmationToUser::class of email_reset
is used as a confirmation email when changing mail.
The template is views\vendor\mail_reset\mail\confirmation.blade.php
Is used. Change according to the specifications of the application.
controller
Example of changing e-mail address
<?php
namespace App\Http\Controllers;
use Auth;
use MailReset;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Kaoken\LaravelMailReset\Controllers\MailResetUsers;
class MailResetController extends Controller
{
use MailResetUsers;
/**
* use trit MailResetUsers
* @var string
*/
protected $broker = 'users';
/**
* Mail address change view
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getChangeMail()
{
// 各自で用意する
return view('change_email');
}
/**
* Change user's email address
* @param Request $request
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function postChangeMail(Request $request)
{
$all = $request->only(['email']);
$validator = Validator::make($all,[
'email' => 'required|unique:users,email|max:255|email',
]);
if ($validator->fails()) {
return redirect('change_email')
->withErrors($validator)
->withInput();
}
switch ( $this->sendMailAddressChangeLink(Auth::guard('customer')->user()->id, $all['email']) ) {
case MailReset::INVALID_USER:
redirect('first_register')
->withErrors(['mail_reset'=>'Invalid user.']);
break;
case MailReset::SAME_EMAIL_EXIST:
redirect('first_register')
->withErrors(['mail_reset'=>'The same mail address already exists.']);
break;
case MailReset::INVALID_CONFIRMATION:
default:
redirect('first_register')
->withErrors(['mail_reset'=>'An unexpected error occurred.']);
}
return redirect('change_email_ok');
}
}
Be sure to write use MailResetUsers and $broker in the class.
Route
From the above controller!
Route::group([
'middleware' => ['auth:user'],
],
function(){
Route::get('user/mail/reset', 'MailResetController@getChangeMail');
Route::post('user/mail/reset', 'MailResetController@postChangeMail');
}
);
Route::get('user/mail/reset/{id}/{email}/{token}', 'MailResetController@getChangeMailAddress');
Events
See inside the vendor\kaoken\laravel-email-reset\src\Events directory!
ChangedMailAddressEvent
Called after the mail address has been completely changed.
MailResetConfirmationEvent
It is called after saving the change candidate of the mail address.
License
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 1.0.3 | >=7.0.0 | 5.5.* | MIT |