実現したいこと
認証をユーザータイプA(userテーブルを使用)か、ユーザータイプB(hr_userテーブルを使用)によって分けたいと思い、マルチ認証を実装しています。
そこで以下のエラーが出て、何が原因か分からず立ち止まっております。
何卒、ご教示のほどよろしくお願いいたします。
エラー内容
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Models\HrUser given, called in C:\Users\〇〇\git\〇〇\vendor\laravel\ui\auth-backend\RegistersUsers.php on line 36
エラーの原因だと思われるソースコード
<auth.php>
php
1<?php 2 3return [ 4 5 'defaults' => [ 6 'guard' => 'web', 7 'passwords' => 'users', 8 ], 9 10 'guards' => [ 11 'web' => [ 12 'driver' => 'session', 13 'provider' => 'users', 14 ], 15 'hr' => [ 16 'driver' => 'session', 17 'provider' => 'hr_users', 18 ], 19 'api' => [ 20 'driver' => 'token', 21 'provider' => 'users', 22 'hash' => false, 23 ], 24 ], 25 26 'providers' => [ 27 'users' => [ 28 'driver' => 'eloquent', 29 'model' => App\Models\User::class, 30 ], 31 'hr_users' => [ 32 'driver' => 'eloquent', 33 'model' => App\HrUser::class, 34 ], 35 36 // 'users' => [ 37 // 'driver' => 'database', 38 // 'table' => 'users', 39 // ], 40 ], 41 42 'passwords' => [ 43 'users' => [ 44 'provider' => 'users', 45 'table' => 'password_resets', 46 'expire' => 60, 47 'throttle' => 60, 48 ], 49 'hr_users' => [ 50 'provider' => 'hr_users', 51 'table' => 'password_resets', 52 'expire' => 60, 53 'throttle' => 60, 54 ], 55 ], 56 57 'password_timeout' => 10800, 58];
<\app\Http\Controllers\Hr\Auth\RegisterController.php>
php
1<?php 2 3namespace App\Http\Controllers\Hr\Auth; 4 5use App\Http\Controllers\Controller; 6use App\Providers\RouteServiceProvider; 7use App\Models\User; 8use App\Models\HrUser; 9use Illuminate\Foundation\Auth\RegistersUsers; 10use Illuminate\Support\Facades\Hash; 11use Illuminate\Support\Facades\Validator; 12use Illuminate\Support\Facades\Auth; 13 14class RegisterController extends Controller 15{ 16 17 use RegistersUsers; 18 public function __construct() 19 { 20 $this->middleware('guest'); 21 } 22 protected function validator(array $data) 23 { 24 return Validator::make($data, [ 25 'name' => ['required', 'string', 'max:255'], 26 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 27 'password' => ['required', 'string', 'min:8', 'confirmed'], 28 ]); 29 } 30 31 protected function create(array $data) 32 { 33 return HrUser::create([ 34 'name' => $data['name'], 35 'username' => $data['username'], 36 'email' => $data['email'], 37 //省略 38 'password' => Hash::make($data['password']), 39 ]); 40 } 41 42 public function showRegistrationForm() 43 { 44 return view('hr.auth.register'); 45 } 46 47 protected function guard(){ 48 return Auth::guard('hr'); 49 } 50} 51
<vendor\laravel\ui\auth-backend\RegistersUsers.php>
php
1 2trait RegistersUsers 3{ 4 use RedirectsUsers; 5 6 /** 7 * Show the application registration form. 8 * 9 * @return \Illuminate\View\View 10 */ 11 public function showRegistrationForm() 12 { 13 return view('auth.register'); 14 } 15 16 /** 17 * Handle a registration request for the application. 18 * 19 * @param \Illuminate\Http\Request $request 20 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse 21 */ 22 public function register(Request $request) 23 { 24 $this->validator($request->all())->validate(); 25 26 event(new Registered($user = $this->create($request->all()))); 27 28 $this->guard()->login($user); 29 30 if ($response = $this->registered($request, $user)) { 31 return $response; 32 } 33 34 return $request->wantsJson() 35 ? new JsonResponse([], 201) 36 : redirect($this->redirectPath()); 37 } 38 39 /** 40 * Get the guard to be used during registration. 41 * 42 * @return \Illuminate\Contracts\Auth\StatefulGuard 43 */ 44 protected function guard() 45 { 46 return Auth::guard(); 47 } 48 49 /** 50 * The user has been registered. 51 * 52 * @param \Illuminate\Http\Request $request 53 * @param mixed $user 54 * @return mixed 55 */ 56 protected function registered(Request $request, $user) 57 { 58 // 59 } 60} 61
<HrUser.php>
php
1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Factories\HasFactory; 6use Illuminate\Database\Eloquent\Model; 7use Illuminate\Notifications\Notifiable; 8 9class HrUser extends Model 10{ 11 use HasFactory, Notifiable; 12 13 /** 14 * The attributes that are mass assignable. 15 * 16 * @var array 17 */ 18 19 //$guarded は変更できないカラム 20 protected $guarded = [ 21 'details_id', 22 ]; 23 24 /** 25 * The attributes that should be hidden for arrays. 26 * 27 * @var array 28 */ 29 protected $hidden = [ 30 'password', 31 'remember_token', 32 ]; 33 34 /** 35 * The attributes that should be cast to native types. 36 * 37 * @var array 38 */ 39 protected $casts = [ 40 'email_verified_at' => 'datetime', 41 ]; 42 43 protected $table = 'hr_users'; 44} 45
回答1件
あなたの回答
tips
プレビュー