質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

1回答

972閲覧

[Laravel5.8]新規ユーザー登録後にプロフィール登録画面に遷移したい

退会済みユーザー

退会済みユーザー

総合スコア0

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2020/07/24 12:41

前提・実現したいこと

ユーザー登録後、プロフィールを設定する画面に遷移したい!

Laravel5.8を使って簡単なユーザー登録、ログイン、ログアウトなどができるシステムを練習のため、構築しています。
※中身は特にまだ決めていはいませんが、簡単なメモ機能にしようと思っています。

php artisan make:authで簡単に認証機能を作り、当初は問題なく動いていましたが、
パスワードリセット機能を作成したあたりから、下記に記載するエラーが発生し、プロフィール画面に遷移しなくなってしまいました。
お手数ですが、お知恵を貸していただければ幸いです。

発生している問題・エラーメッセージ

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given, called in /home/vagrant/code/memo/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 35

該当のソースコード[Illuminate\Auth\SessionGuard::login()]

php

1 /** 2 * Log a user into the application. 3 * 4 * @param \Illuminate\Contracts\Auth\Authenticatable $user 5 * @param bool $remember 6 * @return void 7 */ 8 public function login(AuthenticatableContract $user, $remember = false) 9 { 10 $this->updateSession($user->getAuthIdentifier()); 11 12 // If the user should be permanently "remembered" by the application we will 13 // queue a permanent cookie that contains the encrypted copy of the user 14 // identifier. We will then decrypt this later to retrieve the users. 15 if ($remember) { 16 $this->ensureRememberTokenIsSet($user); 17 18 $this->queueRecallerCookie($user); 19 } 20 21 // If we have an event dispatcher instance set we will fire an event so that 22 // any listeners will hook into the authentication events and run actions 23 // based on the login and logout events fired from the guard instances. 24 $this->fireLoginEvent($user, $remember); 25 26 $this->setUser($user); 27 } 28

該当のソースコード[Illuminate\Contracts\Auth\Authenticatable]

php

1<?php 2 3namespace Illuminate\Contracts\Auth; 4 5interface Authenticatable 6{ 7 /** 8 * Get the name of the unique identifier for the user. 9 * 10 * @return string 11 */ 12 public function getAuthIdentifierName(); 13 14 /** 15 * Get the unique identifier for the user. 16 * 17 * @return mixed 18 */ 19 public function getAuthIdentifier(); 20 21 /** 22 * Get the password for the user. 23 * 24 * @return string 25 */ 26 public function getAuthPassword(); 27 28 /** 29 * Get the token value for the "remember me" session. 30 * 31 * @return string 32 */ 33 public function getRememberToken(); 34 35 /** 36 * Set the token value for the "remember me" session. 37 * 38 * @param string $value 39 * @return void 40 */ 41 public function setRememberToken($value); 42 43 /** 44 * Get the column name for the "remember me" token. 45 * 46 * @return string 47 */ 48 public function getRememberTokenName(); 49} 50

該当のソースコード[Illuminate\Http\RedirectResponse]

php

1<?php 2 3namespace Illuminate\Contracts\Auth; 4 5interface Authenticatable 6{ 7 /** 8 * Get the name of the unique identifier for the user. 9 * 10 * @return string 11 */ 12 public function getAuthIdentifierName(); 13 14 /** 15 * Get the unique identifier for the user. 16 * 17 * @return mixed 18 */ 19 public function getAuthIdentifier(); 20 21 /** 22 * Get the password for the user. 23 * 24 * @return string 25 */ 26 public function getAuthPassword(); 27 28 /** 29 * Get the token value for the "remember me" session. 30 * 31 * @return string 32 */ 33 public function getRememberToken(); 34 35 /** 36 * Set the token value for the "remember me" session. 37 * 38 * @param string $value 39 * @return void 40 */ 41 public function setRememberToken($value); 42 43 /** 44 * Get the column name for the "remember me" token. 45 * 46 * @return string 47 */ 48 public function getRememberTokenName(); 49} 50

該当のソースコード[Illuminate/Foundation/Auth/RegistersUsers]

php

1 /** 2 * Handle a registration request for the application. 3 * 4 * @param \Illuminate\Http\Request $request 5 * @return \Illuminate\Http\Response 6 */ 7 public function register(Request $request) 8 { 9 $this->validator($request->all())->validate(); 10 11 event(new Registered($user = $this->create($request->all()))); 12 13 $this->guard()->login($user); 14 15 return $this->registered($request, $user) 16 ?: redirect($this->redirectPath()); 17 } 18

該当のソースコード[app/User.php]

php

1 <?php 2 3namespace App; 4 5use App\Notifications\CustomPasswordReset; 6use Illuminate\Database\Eloquent\SoftDeletes; 7use Illuminate\Notifications\Notifiable; 8use Illuminate\Contracts\Auth\MustVerifyEmail; 9use Illuminate\Foundation\Auth\User as Authenticatable; 10//use Illuminate\Auth\Authenticatable; 11 12 13class User extends Authenticatable 14{ 15 use Notifiable; 16 use SoftDeletes; 17// use Authenticatable; 18 19 /** 20 * The attributes that are mass assignable. 21 * 22 * @var array 23 */ 24 protected $fillable = [ 25 'email', 'password','img','introduction' 26 ]; 27 28 //userとmemoを結ぶ 29 public function memos() 30 { 31 return $this->hasMany('App\Memo'); 32 } 33 34 /** 35 * The attributes that should be hidden for arrays. 36 * 37 * @var array 38 */ 39 protected $hidden = [ 40 'password', 'remember_token', 41 ]; 42 43 /** 44 * The attributes that should be cast to native types. 45 * 46 * @var array 47 */ 48 protected $casts = [ 49 'email_verified_at' => 'datetime', 50 ]; 51 52 /** 53 * パスワードリセット通知の送信 54 * 55 * @param string $token 56 * @return void 57 */ 58 public function sendPasswordResetNotification($token) 59 { 60 $this->notify(new CustomPasswordReset($token)); 61 } 62} 63 64

試したこと

DBを覗いていみると、ユーザーは登録できているので、登録認証自体には問題ないと考えました。
次に登録されたユーザー情報でプロフィール登録画面を表示してみたら、こちらも問題なく表示されました。
そのため、登録後のプロフィール画面への遷移に問題があると考え、リダイレクトのやり方を変えてみたり、遷移先の画面にユーザー情報を渡してみたりしましたが、こちらでは解決できませんでした。改めてエラーメッセージを確認し、ログインの認証?が渡っていないのかなと思い、ログイン認証を付与しようと調べておりましたが、結局何が原因で、どう解決すれば良いかわかりませんでした。

補足情報(FW/ツールのバージョンなど)

HomesteadによるLAMP環境。Laravel5.8
laravelはMVCでウェブシステムが簡単に作れるため使用していますが、初学者のため、laravel内の詳細な仕組みは理解していません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

公式ドキュメントの認証 パスのカスタマイズという項目に記載してあります。

ユーザーが認証に成功すると、/homeのURIへリダイレクトします。これをカスタマイズするには、LoginController、RegisterController、ResetPasswordController、VerificationControllerのredirectToプロパティで、認証後のリダイレクト先の場所を定義してください。

protected $redirectTo = '/';

次に、ユーザーをリダイレクトさせるときの新しいURIを使用するために、RedirectIfAuthenticatedミドルウェアのhandleメソッドを更新する必要があります。

リダイレクトパスをカスタマイズするロジックが必要な場合は、redirectToプロパティの代わりにredirectToメソッドを定義してください。

protected function redirectTo() { return '/path'; }

投稿2020/08/01 15:29

kai0310

総合スコア2070

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/08/06 02:28

ありがとうございます! こちらを参考にもう一度やってみます!
退会済みユーザー

退会済みユーザー

2020/08/06 04:07

RegisterControllerに本来なら使用できない関数with()を使用しておりました。それが原因でした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問