How to create custom authentication in laravel 8 with a preexisting table (but with table name Users_system not Users) and this table (Users_system) is full of data and does not have the same fields as Users for laravel?
1 Answer
Its not necessary to have same fields in your database. If you want to create a new model for new table, create UserSystem model and replace all the App\User instances with App\UserSystem model.
If you want to use existing model, just change the table name in the App\User model.
If you have different user name or any authentication credentials, you need to move Illuminate\Foundation\Auth\AuthenticatesUsers class code to App\Http\Controllers\Auth\LoginController.
Following are the code segments that you need to change there.
protected function validateLogin(Request $request)
{
$request->validate([
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password');
}
//Mention your username here
public function username()
{
return 'user_name';
}
sessionorcookiesand create own middleware to protect