PiotrG
319
6
Laravel

Login with Account Number, Username and Password

Posted 1 year ago by PiotrG

Hi!

I would like to be able to change the default Auth to use an Account Number and a Username to log in.

I've changed the migration to include accountnumber and username and made a composite unique of those two.

I'm not interested in the register or reset password methods as I will be filling this table in from outside my Laravel App.

I've changed the Login View to include the accountnumber and username text fields.

I've added the following code in LoginController:

/**
 * Get the login username to be used by the controller
 * 
 * @return string
 */
public function username()
{
    return 'username';
}

/**
 * Get the login account number to be used by the controller
 * 
 * @return string
 */
public function accountnumber()
{
    return 'accountnumber';
}

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->accountnumber() => 'required|string',
        $this->username() => 'required|string',
        'password' => 'required|string',
    ]);
}

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function credentials(Request $request)
{
    return $request->only($this->accountnumber(), $this->username(), 'password');
}

I can log in using a username now but it doesn't take the accountnumber field into account. I can enter anything in that field and it still logs in me as long as username and password are correct.

Any ideas?

Please sign in or create an account to participate in this conversation.