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
composer:
composer require kaoken/laravel-email-reset
'providers' => [
...
// add
Kaoken\LaravelMailReset\MailResetServiceProvider::class
],
'aliases' => [
...
// add
'MailReset' => Kaoken\LaravelMailReset\Facades\MailReset::class
],
add 'email_reset' => 'users',
.
[
...
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
// add
'email_reset' => 'users',
],
...
]
model
is a user model classemail_confirmation
should modify the class derived fromMailable as necessary. Used to send confirmation mail.table
is the name of the table used for this service- If
expire
does 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,
]
],
php artisan vendor:publish --tag=mail-reset
After execution, the following directories and files are added.
database
migrations
2017_09_21_000001_create_mail_reset_users_table.php
resources
lang
en
mail_reset.php
ja
mail_reset.php
views
vendor
confirmation
mail
confirmation.blade.php
complete.blade.blade.php
Migration file 2017_09_21_000001_create_mail_reset_users_table.php
should be modified as necessary.
php artisan migrate
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.
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.
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');
See inside the vendor\kaoken\laravel-email-reset\src\Events
directory!
Called after the mail address has been completely changed.
It is called after saving the change candidate of the mail address.