お世話になっております。
Laravel5.8で認証のカスタマイズを行っています。
メール認証のカスタマイズに苦戦しております。
想定しているフローは以下になります。
※前提としてAuth::routes(['verify' => true])のメール認証機能を使用しています。
1.新規会員登録をメールアドレスのみに変更
2.メール送信完了ページ
3.メールに記載されているリンクをクリックし認証後パスワード登録
4.Homeへ移動
今悩んでいる箇所なんですが、
2.メール送信完了ページ
この部分で「メール送信をしました。メールに記載されているリンクをクリックし認証を行ってください」とメール確認を促すページが表示されます。
email/verify
この時点で既にログインされてしまうんです。
自分なりに調べてみましたがこれは仕様のようです。
海外ではメール認証を行う前に一定の領域はログインできる仕様が多いようです。。
今回新規会員登録はメールアドレスのみでして、このタイミングでログインできてしまうのは避けたいです。
代替え案として、監視用のステータスカラムを作成し、ステータスによってマイページへのリンクを隠す、とかは考えたんですが直叩きされれば結局入れてしまうので根本的な解決にならないな、と考えております。
根本解決はmiddlewareの「auth」を「verified」に統一することです。
参考に変更ファイルなど控えておきます。
もし良き方法がありましたら何卒ご教授のほどよろしくお願いいたします。
app/User.php
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements MustVerifyEmail { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['email']; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; }
routes/web.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); // メール認証付きの会員登録 Auth::routes(['verify' => true]); // パスワード登録用コントローラー Route::get('/password_regist', 'PasswordRegistController@index')->name('password_regist'); Route::post('/password_regist', 'PasswordRegistController@create')->name('password_create'); Route::get('/home', 'HomeController@index')->name('home');
database/migrations/000000_create_users_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id')->comment('ID'); $table->string('user_name')->nullable()->comment('ユーザー名'); $table->string('email')->unique()->comment('メールアドレス'); $table->timestamp('email_verified_at')->nullable()->comment('メール認証用'); $table->string('password')->nullable()->comment('パスワード'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
app/Http/Controllers/Auth/VerificationController.php
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\VerifiesEmails; use Illuminate\Http\Request; class VerificationController extends Controller { /* |-------------------------------------------------------------------------- | Email Verification Controller |-------------------------------------------------------------------------- | | This controller is responsible for handling email verification for any | user that recently registered with the application. Emails may also | be re-sent if the user didn't receive the original email message. | */ use VerifiesEmails; /** * Where to redirect users after verification. * * @var string */ protected $redirectTo = "/password_regist"; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); $this->middleware('signed')->only('verify'); $this->middleware('throttle:6,1')->only('verify', 'resend'); } }
app/Http/Controllers/PasswordRegistController.php
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; class PasswordRegistController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('verified'); } public function index() { $password = Auth::user()->password; if($password){ return redirect('/home'); } Session::flash('flash_message', 'メールアドレスの認証が完了しました。'); return view('password_regist'); } public function create(Request $request) { $request->validate([ 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); $user = User::find($request->user_id); $user->password = Hash::make($request->password); $user->save(); return redirect('/home'); } }
app/Http/Controllers/HomeController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('verified'); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('home'); } }
あなたの回答
tips
プレビュー