2

I want to use only CPF number to let the login. CPF is like a Social Secure Number or else alike, and that´s a unique number in Brazil. I already have a data table with this number, so no registration needed.

Where do I change the code to accept CPF instead email and password, please?

Thanks!

Marcello Pato
  • 500
  • 6
  • 21
  • It doesn't need a password?? Can log into anyones account? – emotality Jan 18 '19 at 17:26
  • Well, the user doesn´t have a password at this moment yet. But there is another two data that I can use, like "group" and "quota". Anyways, those fields do not default to Laravel. So, I will have to use not the default fields. – Marcello Pato Jan 18 '19 at 17:34
  • Updated my answer, tested it locally and works. :) – emotality Jan 18 '19 at 18:14

2 Answers2

1

You will have to override these methods in your LoginController, just paste this in and change accordingly.

Change default email to cpf field name:

public function username()
{
    return 'cpf'; // or whatever field you use to login
}

Do validation here with credentials:

protected function validateLogin(Request $request)
{
    $request->validate([
        $this->username() => 'required|string|exists:users,cpf',
    ]);
}

Do the login request. Here we look for user then log him in manually and redirect you to the dashboard (home), if it fails, you will be redirected back with the input that was inserted into the form:

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

    $user = User::where('cpf', $request->cpf)->first();

    if ($user) {
        Auth::login($user);
        return redirect()->intended('home');
    }

    return redirect()->back()->withInput($request->all());
}

DB table I tested with:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('cpf')->unique();
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->rememberToken();
    $table->timestamps();
});
emotality
  • 12,795
  • 4
  • 39
  • 60
1

Step 1: open file AuthenticatesUsers.php

Step 2: update the username() function, default you will see email. you need to update it to cpf.

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

Step 3: update the credentials(Request $request) function, by default it is password. you can change as per your approach. if you do not want it,you can remove field for password too!

    protected function credentials(Request $request)
    {
        //return $request->only($this->username(), 'password');
        return $request->only($this->username());
    }
Nayeem Azad
  • 657
  • 5
  • 20
  • Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again." – Marcello Pato Jan 18 '19 at 18:08
  • This may be another issue. you can read answers of [This Question](https://stackoverflow.com/questions/52583886/post-request-in-laravel-5-7-error-419-sorry-your-session-has-expired) – Nayeem Azad Jan 18 '19 at 18:37
  • Probably. I have installed "backpack" before trying to do it. I think will rm -R and restart but now, coding the front-login first – Marcello Pato Jan 18 '19 at 19:02
  • YES! That´s the problem. Reinstall all from stretch and works like a breeze! – Marcello Pato Jan 18 '19 at 19:52