Laravel 8 Custom Password Field For Authentication
I have an old app I am trying to port to Laravel 8. I am trying to use the built in authentication methods without using any of the starter kits or building models.
In the folder app/config/auth.php I have set my users provider for the database.
I am just trying to avoid creating models in Laravel as this is an old project already with a lot of data and I feel like it would be faster to go around that.
LaravelPHP
Last Comment
Scott Fell
8/22/2022 - Mon
Scott Fell
ASKER
Setting app/config/auth.php users to look for the database and specify the users table as opposed to using eloquent was the first part of the solution. The second was to specify the field names to use for the authentication as I did in line 32.
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\Auth;class LoginController extends Controller{ /** * Handle an authentication attempt. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function authenticate(Request $request) { //https://laravel.com/docs/8.x/authentication#authenticating-users if (Auth::attempt(['username' => $request->input("username"), 'CustomPasswordField' => $request->input('password'), 'UserLevel' => 'Admin'], 'Status' => 'active')) { $request->session()->regenerate(); return redirect()->intended('dashboard'); } /* if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended('dashboard'); } */ return back()->withErrors([ 'email' => 'The provided credentials do not match our records.', ]); }}
Open in new window