質問編集履歴

2

タイトル変更

2019/09/26 05:51

投稿

paranoaman1217
paranoaman1217

スコア24

test CHANGED
@@ -1 +1 @@
1
- Laravel5.8 メール認証のカスタマイズ
1
+ Laravel メール認証のmiddlewareをverified統一した
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Laravel5.8で認証のカスタマイズを行っています。
4
4
 
5
- メール認証の挙動がイマイチ分からず苦戦しております。
5
+ メール認証のカスタマイズに苦戦しております。
6
6
 
7
7
 
8
8
 
@@ -42,17 +42,7 @@
42
42
 
43
43
 
44
44
 
45
- 既存のmake:authだと新規会員登録後はHomeにログインしている作りなので、
46
-
47
- その機能のままmiddlewareの「auth」が効いてしまっているんだと予想しているんですが、
48
-
49
- 何か記述が足りないか、そういうものなのかすら分かりません。。
50
-
51
- メール認証後にログインする仕様に変更したいです。(イメージとしてはmiddlewareの「auth」を「verified」にごっそり変更したい)
52
-
53
-
54
-
55
- 自分なりに調べてみましたが仕様のようです。
45
+ 自分なりに調べてみましたがこれは仕様のようです。
56
46
 
57
47
  海外ではメール認証を行う前に一定の領域はログインできる仕様が多いようです。。
58
48
 
@@ -62,6 +52,8 @@
62
52
 
63
53
  代替え案として、監視用のステータスカラムを作成し、ステータスによってマイページへのリンクを隠す、とかは考えたんですが直叩きされれば結局入れてしまうので根本的な解決にならないな、と考えております。
64
54
 
55
+ 根本解決はmiddlewareの「auth」を「verified」に統一することです。
56
+
65
57
 
66
58
 
67
59
  参考に変更ファイルなど控えておきます。

1

自分なりに調べてみた結果を追記しました。

2019/09/26 05:51

投稿

paranoaman1217
paranoaman1217

スコア24

test CHANGED
@@ -1 +1 @@
1
- Laravel5.8 メール認証の挙動について
1
+ Laravel5.8 メール認証のカスタマイズについて
test CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  Laravel5.8で認証のカスタマイズを行っています。
4
4
 
5
- make:authの挙動がイマイチ分からず苦戦しております。
5
+ メール認証の挙動がイマイチ分からず苦戦しております。
6
-
7
-
8
-
6
+
7
+
8
+
9
- カスタマイズいフローは以下になります。
9
+ 想定フローは以下になります。
10
-
10
+
11
- ※前提としてAuth::routes(['verify' => true])のメール認証機能を使用す
11
+ ※前提としてAuth::routes(['verify' => true])のメール認証機能を使用しています。
12
12
 
13
13
 
14
14
 
@@ -50,13 +50,321 @@
50
50
 
51
51
  メール認証後にログインする仕様に変更したいです。(イメージとしてはmiddlewareの「auth」を「verified」にごっそり変更したい)
52
52
 
53
-
54
-
55
- 試しに、app/Http/Controllers/Auth/VerificationController.php
56
-
57
-
53
+
54
+
58
-
55
+ 自分なりに調べてみましたが仕様のようです。
56
+
57
+ 海外ではメール認証を行う前に一定の領域はログインできる仕様が多いようです。。
58
+
59
+ 今回新規会員登録はメールアドレスのみでして、このタイミングでログインできてしまうのは避けたいです。
60
+
61
+
62
+
63
+ 代替え案として、監視用のステータスカラムを作成し、ステータスによってマイページへのリンクを隠す、とかは考えたんですが直叩きされれば結局入れてしまうので根本的な解決にならないな、と考えております。
64
+
65
+
66
+
67
+ 参考に変更ファイルなど控えておきます。
68
+
69
+ もし良き方法がありましたら何卒ご教授のほどよろしくお願いいたします。
70
+
71
+
72
+
73
+ app/User.php
74
+
59
- ```
75
+ ```
76
+
77
+ <?php
78
+
79
+
80
+
81
+ namespace App;
82
+
83
+
84
+
85
+ use Illuminate\Notifications\Notifiable;
86
+
87
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
88
+
89
+ use Illuminate\Foundation\Auth\User as Authenticatable;
90
+
91
+
92
+
93
+ class User extends Authenticatable implements MustVerifyEmail
94
+
95
+ {
96
+
97
+ use Notifiable;
98
+
99
+
100
+
101
+ /**
102
+
103
+ * The attributes that are mass assignable.
104
+
105
+ *
106
+
107
+ * @var array
108
+
109
+ */
110
+
111
+ protected $fillable = ['email'];
112
+
113
+
114
+
115
+ /**
116
+
117
+ * The attributes that should be hidden for arrays.
118
+
119
+ *
120
+
121
+ * @var array
122
+
123
+ */
124
+
125
+ protected $hidden = [
126
+
127
+ 'password', 'remember_token',
128
+
129
+ ];
130
+
131
+
132
+
133
+ /**
134
+
135
+ * The attributes that should be cast to native types.
136
+
137
+ *
138
+
139
+ * @var array
140
+
141
+ */
142
+
143
+ protected $casts = [
144
+
145
+ 'email_verified_at' => 'datetime',
146
+
147
+ ];
148
+
149
+ }
150
+
151
+
152
+
153
+ ```
154
+
155
+
156
+
157
+ routes/web.php
158
+
159
+ ```
160
+
161
+ <?php
162
+
163
+
164
+
165
+ /*
166
+
167
+ |--------------------------------------------------------------------------
168
+
169
+ | Web Routes
170
+
171
+ |--------------------------------------------------------------------------
172
+
173
+ |
174
+
175
+ | Here is where you can register web routes for your application. These
176
+
177
+ | routes are loaded by the RouteServiceProvider within a group which
178
+
179
+ | contains the "web" middleware group. Now create something great!
180
+
181
+ |
182
+
183
+ */
184
+
185
+
186
+
187
+ Route::get('/', function () {
188
+
189
+ return view('welcome');
190
+
191
+ });
192
+
193
+
194
+
195
+ // メール認証付きの会員登録
196
+
197
+ Auth::routes(['verify' => true]);
198
+
199
+
200
+
201
+ // パスワード登録用コントローラー
202
+
203
+ Route::get('/password_regist', 'PasswordRegistController@index')->name('password_regist');
204
+
205
+ Route::post('/password_regist', 'PasswordRegistController@create')->name('password_create');
206
+
207
+
208
+
209
+ Route::get('/home', 'HomeController@index')->name('home');
210
+
211
+
212
+
213
+ ```
214
+
215
+
216
+
217
+ database/migrations/000000_create_users_table.php
218
+
219
+ ```
220
+
221
+ <?php
222
+
223
+
224
+
225
+ use Illuminate\Support\Facades\Schema;
226
+
227
+ use Illuminate\Database\Schema\Blueprint;
228
+
229
+ use Illuminate\Database\Migrations\Migration;
230
+
231
+
232
+
233
+ class CreateUsersTable extends Migration
234
+
235
+ {
236
+
237
+ /**
238
+
239
+ * Run the migrations.
240
+
241
+ *
242
+
243
+ * @return void
244
+
245
+ */
246
+
247
+ public function up()
248
+
249
+ {
250
+
251
+ Schema::create('users', function (Blueprint $table) {
252
+
253
+ $table->bigIncrements('id')->comment('ID');
254
+
255
+ $table->string('user_name')->nullable()->comment('ユーザー名');
256
+
257
+ $table->string('email')->unique()->comment('メールアドレス');
258
+
259
+ $table->timestamp('email_verified_at')->nullable()->comment('メール認証用');
260
+
261
+ $table->string('password')->nullable()->comment('パスワード');
262
+
263
+ $table->rememberToken();
264
+
265
+ $table->timestamps();
266
+
267
+ });
268
+
269
+ }
270
+
271
+
272
+
273
+ /**
274
+
275
+ * Reverse the migrations.
276
+
277
+ *
278
+
279
+ * @return void
280
+
281
+ */
282
+
283
+ public function down()
284
+
285
+ {
286
+
287
+ Schema::dropIfExists('users');
288
+
289
+ }
290
+
291
+ }
292
+
293
+
294
+
295
+ ```
296
+
297
+ app/Http/Controllers/Auth/VerificationController.php
298
+
299
+ ```
300
+
301
+ <?php
302
+
303
+
304
+
305
+ namespace App\Http\Controllers\Auth;
306
+
307
+
308
+
309
+ use App\Http\Controllers\Controller;
310
+
311
+ use Illuminate\Foundation\Auth\VerifiesEmails;
312
+
313
+ use Illuminate\Http\Request;
314
+
315
+
316
+
317
+ class VerificationController extends Controller
318
+
319
+ {
320
+
321
+ /*
322
+
323
+ |--------------------------------------------------------------------------
324
+
325
+ | Email Verification Controller
326
+
327
+ |--------------------------------------------------------------------------
328
+
329
+ |
330
+
331
+ | This controller is responsible for handling email verification for any
332
+
333
+ | user that recently registered with the application. Emails may also
334
+
335
+ | be re-sent if the user didn't receive the original email message.
336
+
337
+ |
338
+
339
+ */
340
+
341
+ use VerifiesEmails;
342
+
343
+
344
+
345
+ /**
346
+
347
+ * Where to redirect users after verification.
348
+
349
+ *
350
+
351
+ * @var string
352
+
353
+ */
354
+
355
+ protected $redirectTo = "/password_regist";
356
+
357
+
358
+
359
+ /**
360
+
361
+ * Create a new controller instance.
362
+
363
+ *
364
+
365
+ * @return void
366
+
367
+ */
60
368
 
61
369
  public function __construct()
62
370
 
@@ -70,37 +378,109 @@
70
378
 
71
379
  }
72
380
 
381
+ }
382
+
383
+
384
+
73
- ```
385
+ ```
74
-
386
+
75
-
387
+ app/Http/Controllers/PasswordRegistController.php
76
-
388
+
77
- ```
389
+ ```
390
+
391
+ <?php
392
+
393
+
394
+
395
+ namespace App\Http\Controllers;
396
+
397
+
398
+
399
+ use App\User;
400
+
401
+ use Illuminate\Http\Request;
402
+
403
+ use Illuminate\Support\Facades\Hash;
404
+
405
+ use Illuminate\Support\Facades\Auth;
406
+
407
+ use Illuminate\Support\Facades\Session;
408
+
409
+
410
+
411
+ class PasswordRegistController extends Controller
412
+
413
+ {
414
+
415
+ /**
416
+
417
+ * Create a new controller instance.
418
+
419
+ *
420
+
421
+ * @return void
422
+
423
+ */
78
424
 
79
425
  public function __construct()
80
426
 
81
427
  {
82
428
 
83
- $this->middleware('guest');
429
+ $this->middleware('verified');
84
-
85
- $this->middleware('signed')->only('verify');
430
+
86
-
87
- $this->middleware('throttle:6,1')->only('verify', 'resend');
88
-
89
- }
431
+ }
432
+
433
+
434
+
90
-
435
+ public function index()
436
+
437
+ {
438
+
439
+ $password = Auth::user()->password;
440
+
441
+ if($password){
442
+
443
+ return redirect('/home');
444
+
445
+ }
446
+
447
+ Session::flash('flash_message', 'メールアドレスの認証が完了しました。');
448
+
449
+ return view('password_regist');
450
+
451
+ }
452
+
453
+
454
+
455
+ public function create(Request $request)
456
+
457
+ {
458
+
459
+ $request->validate([
460
+
461
+ 'password' => ['required', 'string', 'min:8', 'confirmed'],
462
+
463
+ ]);
464
+
465
+
466
+
467
+ $user = User::find($request->user_id);
468
+
469
+ $user->password = Hash::make($request->password);
470
+
471
+ $user->save();
472
+
473
+ return redirect('/home');
474
+
475
+ }
476
+
477
+ }
478
+
479
+
480
+
91
- ```
481
+ ```
92
-
93
-
94
-
95
- に変更するとエラーになります。
482
+
96
-
97
- 参考に変更ファイルなど控えておきます。
98
-
99
- 何卒ご教授のほどよろしくお願いします。
100
-
101
-
102
-
103
- app/User.php
483
+ app/Http/Controllers/HomeController.php
104
484
 
105
485
  ```
106
486
 
@@ -108,470 +488,60 @@
108
488
 
109
489
 
110
490
 
111
- namespace App;
491
+ namespace App\Http\Controllers;
112
-
113
-
114
-
492
+
493
+
494
+
115
- use Illuminate\Notifications\Notifiable;
495
+ use Illuminate\Http\Request;
116
-
117
- use Illuminate\Contracts\Auth\MustVerifyEmail;
496
+
118
-
119
- use Illuminate\Foundation\Auth\User as Authenticatable;
497
+
120
-
121
-
122
-
498
+
123
- class User extends Authenticatable implements MustVerifyEmail
499
+ class HomeController extends Controller
124
500
 
125
501
  {
126
502
 
127
- use Notifiable;
128
-
129
-
130
-
131
- /**
503
+ /**
132
-
504
+
133
- * The attributes that are mass assignable.
505
+ * Create a new controller instance.
134
-
506
+
135
- *
507
+ *
136
-
508
+
137
- * @var array
509
+ * @return void
138
-
510
+
139
- */
511
+ */
512
+
140
-
513
+ public function __construct()
514
+
515
+ {
516
+
141
- protected $fillable = ['email'];
517
+ $this->middleware('verified');
518
+
142
-
519
+ }
143
-
144
-
520
+
521
+
522
+
145
- /**
523
+ /**
146
-
524
+
147
- * The attributes that should be hidden for arrays.
525
+ * Show the application dashboard.
148
-
526
+
149
- *
527
+ *
150
-
528
+
151
- * @var array
529
+ * @return \Illuminate\Contracts\Support\Renderable
152
-
530
+
153
- */
531
+ */
154
-
532
+
155
- protected $hidden = [
533
+ public function index()
534
+
156
-
535
+ {
536
+
157
- 'password', 'remember_token',
537
+ return view('home');
158
-
159
- ];
538
+
160
-
161
-
162
-
163
- /**
164
-
165
- * The attributes that should be cast to native types.
166
-
167
- *
539
+ }
168
-
169
- * @var array
540
+
170
-
171
- */
541
+
172
-
173
- protected $casts = [
174
-
175
- 'email_verified_at' => 'datetime',
176
-
177
- ];
178
542
 
179
543
  }
180
544
 
181
545
 
182
546
 
183
547
  ```
184
-
185
-
186
-
187
- routes/web.php
188
-
189
- ```
190
-
191
- <?php
192
-
193
-
194
-
195
- /*
196
-
197
- |--------------------------------------------------------------------------
198
-
199
- | Web Routes
200
-
201
- |--------------------------------------------------------------------------
202
-
203
- |
204
-
205
- | Here is where you can register web routes for your application. These
206
-
207
- | routes are loaded by the RouteServiceProvider within a group which
208
-
209
- | contains the "web" middleware group. Now create something great!
210
-
211
- |
212
-
213
- */
214
-
215
-
216
-
217
- Route::get('/', function () {
218
-
219
- return view('welcome');
220
-
221
- });
222
-
223
-
224
-
225
- // メール認証付きの会員登録
226
-
227
- Auth::routes(['verify' => true]);
228
-
229
-
230
-
231
- // パスワード登録用コントローラー
232
-
233
- Route::get('/password_regist', 'PasswordRegistController@index')->name('password_regist');
234
-
235
- Route::post('/password_regist', 'PasswordRegistController@create')->name('password_create');
236
-
237
-
238
-
239
- Route::get('/home', 'HomeController@index')->name('home');
240
-
241
-
242
-
243
- ```
244
-
245
-
246
-
247
- database/migrations/000000_create_users_table.php
248
-
249
- ```
250
-
251
- <?php
252
-
253
-
254
-
255
- use Illuminate\Support\Facades\Schema;
256
-
257
- use Illuminate\Database\Schema\Blueprint;
258
-
259
- use Illuminate\Database\Migrations\Migration;
260
-
261
-
262
-
263
- class CreateUsersTable extends Migration
264
-
265
- {
266
-
267
- /**
268
-
269
- * Run the migrations.
270
-
271
- *
272
-
273
- * @return void
274
-
275
- */
276
-
277
- public function up()
278
-
279
- {
280
-
281
- Schema::create('users', function (Blueprint $table) {
282
-
283
- $table->bigIncrements('id')->comment('ID');
284
-
285
- $table->string('user_name')->nullable()->comment('ユーザー名');
286
-
287
- $table->string('email')->unique()->comment('メールアドレス');
288
-
289
- $table->timestamp('email_verified_at')->nullable()->comment('メール認証用');
290
-
291
- $table->string('password')->nullable()->comment('パスワード');
292
-
293
- $table->rememberToken();
294
-
295
- $table->timestamps();
296
-
297
- });
298
-
299
- }
300
-
301
-
302
-
303
- /**
304
-
305
- * Reverse the migrations.
306
-
307
- *
308
-
309
- * @return void
310
-
311
- */
312
-
313
- public function down()
314
-
315
- {
316
-
317
- Schema::dropIfExists('users');
318
-
319
- }
320
-
321
- }
322
-
323
-
324
-
325
- ```
326
-
327
- app/Http/Controllers/Auth/VerificationController.php
328
-
329
- ```
330
-
331
- <?php
332
-
333
-
334
-
335
- namespace App\Http\Controllers\Auth;
336
-
337
-
338
-
339
- use App\Http\Controllers\Controller;
340
-
341
- use Illuminate\Foundation\Auth\VerifiesEmails;
342
-
343
- use Illuminate\Http\Request;
344
-
345
-
346
-
347
- class VerificationController extends Controller
348
-
349
- {
350
-
351
- /*
352
-
353
- |--------------------------------------------------------------------------
354
-
355
- | Email Verification Controller
356
-
357
- |--------------------------------------------------------------------------
358
-
359
- |
360
-
361
- | This controller is responsible for handling email verification for any
362
-
363
- | user that recently registered with the application. Emails may also
364
-
365
- | be re-sent if the user didn't receive the original email message.
366
-
367
- |
368
-
369
- */
370
-
371
- use VerifiesEmails;
372
-
373
-
374
-
375
- /**
376
-
377
- * Where to redirect users after verification.
378
-
379
- *
380
-
381
- * @var string
382
-
383
- */
384
-
385
- protected $redirectTo = "/password_regist";
386
-
387
-
388
-
389
- /**
390
-
391
- * Create a new controller instance.
392
-
393
- *
394
-
395
- * @return void
396
-
397
- */
398
-
399
- public function __construct()
400
-
401
- {
402
-
403
- $this->middleware('auth');
404
-
405
- $this->middleware('signed')->only('verify');
406
-
407
- $this->middleware('throttle:6,1')->only('verify', 'resend');
408
-
409
- }
410
-
411
- }
412
-
413
-
414
-
415
- ```
416
-
417
- app/Http/Controllers/PasswordRegistController.php
418
-
419
- ```
420
-
421
- <?php
422
-
423
-
424
-
425
- namespace App\Http\Controllers;
426
-
427
-
428
-
429
- use App\User;
430
-
431
- use Illuminate\Http\Request;
432
-
433
- use Illuminate\Support\Facades\Hash;
434
-
435
- use Illuminate\Support\Facades\Auth;
436
-
437
- use Illuminate\Support\Facades\Session;
438
-
439
-
440
-
441
- class PasswordRegistController extends Controller
442
-
443
- {
444
-
445
- /**
446
-
447
- * Create a new controller instance.
448
-
449
- *
450
-
451
- * @return void
452
-
453
- */
454
-
455
- public function __construct()
456
-
457
- {
458
-
459
- $this->middleware('verified');
460
-
461
- }
462
-
463
-
464
-
465
- public function index()
466
-
467
- {
468
-
469
- $password = Auth::user()->password;
470
-
471
- if($password){
472
-
473
- return redirect('/home');
474
-
475
- }
476
-
477
- Session::flash('flash_message', 'メールアドレスの認証が完了しました。');
478
-
479
- return view('password_regist');
480
-
481
- }
482
-
483
-
484
-
485
- public function create(Request $request)
486
-
487
- {
488
-
489
- $request->validate([
490
-
491
- 'password' => ['required', 'string', 'min:8', 'confirmed'],
492
-
493
- ]);
494
-
495
-
496
-
497
- $user = User::find($request->user_id);
498
-
499
- $user->password = Hash::make($request->password);
500
-
501
- $user->save();
502
-
503
- return redirect('/home');
504
-
505
- }
506
-
507
- }
508
-
509
-
510
-
511
- ```
512
-
513
- app/Http/Controllers/HomeController.php
514
-
515
- ```
516
-
517
- <?php
518
-
519
-
520
-
521
- namespace App\Http\Controllers;
522
-
523
-
524
-
525
- use Illuminate\Http\Request;
526
-
527
-
528
-
529
- class HomeController extends Controller
530
-
531
- {
532
-
533
- /**
534
-
535
- * Create a new controller instance.
536
-
537
- *
538
-
539
- * @return void
540
-
541
- */
542
-
543
- public function __construct()
544
-
545
- {
546
-
547
- $this->middleware('verified');
548
-
549
- }
550
-
551
-
552
-
553
- /**
554
-
555
- * Show the application dashboard.
556
-
557
- *
558
-
559
- * @return \Illuminate\Contracts\Support\Renderable
560
-
561
- */
562
-
563
- public function index()
564
-
565
- {
566
-
567
- return view('home');
568
-
569
- }
570
-
571
-
572
-
573
- }
574
-
575
-
576
-
577
- ```