Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sarathiscookie's avatar

How can we use our own database fields for auth login in laravel?

I used a login method to change md5 to bcrypt without disturbing users. But how can we use our own database field names (usrEmail and usrPassword). At-present login will works if fields are email and password.

LoginController.php

public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        // check against old md5 password, if correct, create bcrypted updated pswd
        $user = User::where('usrEmail', $request->email)
            ->whereIn('usrlId', [1, 2, 5, 6])
            ->first();

        if( $user && $user->usrPassword == md5('aFGQ475SDsdfsaf2342'. $request->password. $user->usrPasswordSalt) )
        {
            $user->usrPassword = Hash::make($request->password);
            $user->save();
        }


        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }
0 likes
7 replies
bashy's avatar

What is it stopping at? Validation?

Devmaurice's avatar

you can validate aganist your fields as you may wish. Just use :

 Auth::attempt([fields to auth aganist])

Example:

$credentials = array(
        'username' => request('email'),
        'password' => md5(request('password'))
    );

    if(Auth::attempt($credentials))
    {
        return "User has been logged in.";
    }
    else
    {
        //return Redirect::back()->with_input();
        return "User has not been logged in.";
    }    
sarathiscookie's avatar

@bashy If I change database fields to email and password then I added code like below login is working perfectly.

$user = User::where('email', $request->email)
            ->whereIn('usrlId', [1, 2, 5, 6])
            ->first();

        if( $user && $user->email== md5('aFGQ475SDsdfsaf2342'. $request->password. $user->usrPasswordSalt) )
        {
            $user->usrPassword= Hash::make($request->password);
            $user->save();
        }

But login is not working with database fields (usrEmail and usrPassword). If I tried to login it is redirect back in to the same login page without error message.

jcmargentina's avatar

my friend, everything is in the official documentation. Is really easy to follow.

biishmar's avatar

In logincontroller

public function username()
{
    return 'usrEmail';
}

But password column should named as password, no options here, but still u want to proceed, there is an another way

$user = User::where('email', $request->email)
            ->whereIn('usrlId', [1, 2, 5, 6])
            ->first();

        if( $user && $user->email== md5('aFGQ475SDsdfsaf2342'. $request->password. $user->usrPasswordSalt) )
        {
            $user->email= Hash::make($request->password);
            $user->save();
        }

Auth::loginUsingId($user->id);
ProComputerLLC's avatar

The stock Laravel Auth login uses the same 'email', 'password' element names for the view script as for the database column(field) names. I want to use 'txtEmail' as my login form email element name while preserving the stock 'email' column name. The solution is to override the 'credentials()' method and change the credential name from 'txtEmail' to the database column name 'email'

/**
 * OVERRIDDEN from AuthenticatesUsers - Get the login username to be used
 * by the controller.
 *
 * Override to specify the 'email' form element
 * name used in the view script e.g. 'login.blade.php'
 * Change from the default element name 'email' to
 * element name 'txtEmail' used in the HTML email
 * element 'login.blade.php'
 *
 * @return string
 */
public function username() {
    return 'txtEmail';
}
/**
 * OVERRIDDEN from AuthenticatesUsers - Get the needed authorization credentials
 * from the request.
 *
 * Override to replace the email form element name used
 * in the 'login.blade.php' view script ('txtEmail')
 * with the database column(field) name 'email'
 *
 * The login username above ('txtEmail') used in the
 * 'name' attribute of the login view script email
 * element is not the same as the database column(field)
 * ('email') in the 'users' table.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function credentials(Request $request) {
    // Get the user name, in this case 'txtEmail'
    $userName = $this->username();
    // Get name=>value pairs of REQUEST elements for username
    // and password.
    $credentials = $request->only($userName, 'password');
    // Extract the email value e.g. '[email protected]'
    $email = $credentials[$userName];
    // Remove it from the credentials
    unset($credentials[$userName]);
    // Add the the email value whose key is the database
    // column(field) name 'email'.
    $credentials['email'] = $email;
    return $credentials;
}

Please or to participate in this conversation.