Avatar of Scott Fell
Scott Fell
Flag for United States of America asked on

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.
  'providers' => [
        //'users' => [
        //    'driver' => 'eloquent',
        //    'model' => App\Models\User::class,
        //],


        'users' => [
             'driver' => 'database',
             'table' => 'tLogin',
         ],
    ],

Open in new window

The password field is not 'password' and I am trying to figure out how to override using the default 'password' name.

As an example, for the login controller, I am getting an error that 'password' does not exist. 
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
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)
    {
        $credentials = $request->only('username', 'password');


        if (Auth::attempt($credentials)) {
            $request->session()->regenerate();

            return redirect()->intended('login');
        }

        return back()->withErrors([
            'email' => 'The provided credentials do not match our records.',
        ]);
    }

}

Open in new window

Should I instead use something like below where I can grab the field names I need for $email and $password like below?
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{    return Redirect::intended('dashboard');
}

Open in new window


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

Avatar of undefined
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.

<?php


namespace 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


ASKER CERTIFIED SOLUTION
Scott Fell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61