Laravel: How do I use other data than email and password?












1















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!










share|improve this question























  • It doesn't need a password?? Can log into anyones account?

    – emotality
    Jan 18 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 Patto
    Jan 18 at 17:34











  • Updated my answer, tested it locally and works. :)

    – emotality
    Jan 18 at 18:14
















1















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!










share|improve this question























  • It doesn't need a password?? Can log into anyones account?

    – emotality
    Jan 18 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 Patto
    Jan 18 at 17:34











  • Updated my answer, tested it locally and works. :)

    – emotality
    Jan 18 at 18:14














1












1








1








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!










share|improve this question














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!







laravel authentication login






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 18 at 17:24









Marcello PattoMarcello Patto

5310




5310













  • It doesn't need a password?? Can log into anyones account?

    – emotality
    Jan 18 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 Patto
    Jan 18 at 17:34











  • Updated my answer, tested it locally and works. :)

    – emotality
    Jan 18 at 18:14



















  • It doesn't need a password?? Can log into anyones account?

    – emotality
    Jan 18 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 Patto
    Jan 18 at 17:34











  • Updated my answer, tested it locally and works. :)

    – emotality
    Jan 18 at 18:14

















It doesn't need a password?? Can log into anyones account?

– emotality
Jan 18 at 17:26





It doesn't need a password?? Can log into anyones account?

– emotality
Jan 18 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 Patto
Jan 18 at 17:34





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 Patto
Jan 18 at 17:34













Updated my answer, tested it locally and works. :)

– emotality
Jan 18 at 18:14





Updated my answer, tested it locally and works. :)

– emotality
Jan 18 at 18:14












2 Answers
2






active

oldest

votes


















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





share|improve this answer

































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





    share|improve this answer
























    • Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again."

      – Marcello Patto
      Jan 18 at 18:08











    • This may be another issue. you can read answers of This Question

      – e2e
      Jan 18 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 Patto
      Jan 18 at 19:02











    • YES! That´s the problem. Reinstall all from stretch and works like a breeze!

      – Marcello Patto
      Jan 18 at 19:52











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258757%2flaravel-how-do-i-use-other-data-than-email-and-password%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









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





    share|improve this answer






























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





      share|improve this answer




























        1












        1








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





        share|improve this answer















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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 18 at 18:13

























        answered Jan 18 at 17:40









        emotalityemotality

        9,57242745




        9,57242745

























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





            share|improve this answer
























            • Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again."

              – Marcello Patto
              Jan 18 at 18:08











            • This may be another issue. you can read answers of This Question

              – e2e
              Jan 18 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 Patto
              Jan 18 at 19:02











            • YES! That´s the problem. Reinstall all from stretch and works like a breeze!

              – Marcello Patto
              Jan 18 at 19:52
















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





            share|improve this answer
























            • Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again."

              – Marcello Patto
              Jan 18 at 18:08











            • This may be another issue. you can read answers of This Question

              – e2e
              Jan 18 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 Patto
              Jan 18 at 19:02











            • YES! That´s the problem. Reinstall all from stretch and works like a breeze!

              – Marcello Patto
              Jan 18 at 19:52














            1












            1








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





            share|improve this answer













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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 18 at 17:42









            e2ee2e

            8112




            8112













            • Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again."

              – Marcello Patto
              Jan 18 at 18:08











            • This may be another issue. you can read answers of This Question

              – e2e
              Jan 18 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 Patto
              Jan 18 at 19:02











            • YES! That´s the problem. Reinstall all from stretch and works like a breeze!

              – Marcello Patto
              Jan 18 at 19:52



















            • Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again."

              – Marcello Patto
              Jan 18 at 18:08











            • This may be another issue. you can read answers of This Question

              – e2e
              Jan 18 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 Patto
              Jan 18 at 19:02











            • YES! That´s the problem. Reinstall all from stretch and works like a breeze!

              – Marcello Patto
              Jan 18 at 19:52

















            Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again."

            – Marcello Patto
            Jan 18 at 18:08





            Doesn't work. It returns a 419 error "Sorry, your session has expired. Please refresh and try again."

            – Marcello Patto
            Jan 18 at 18:08













            This may be another issue. you can read answers of This Question

            – e2e
            Jan 18 at 18:37







            This may be another issue. you can read answers of This Question

            – e2e
            Jan 18 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 Patto
            Jan 18 at 19:02





            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 Patto
            Jan 18 at 19:02













            YES! That´s the problem. Reinstall all from stretch and works like a breeze!

            – Marcello Patto
            Jan 18 at 19:52





            YES! That´s the problem. Reinstall all from stretch and works like a breeze!

            – Marcello Patto
            Jan 18 at 19:52


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258757%2flaravel-how-do-i-use-other-data-than-email-and-password%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Callistus III

            Ostreoida

            Plistias Cous