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);
}
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.
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;
}