User cannot login after registering on Laravel

I am trying to set up an authentication system on Laravel from scratch, but I cannot get the user to log in after the logged in user.

My RegisterController to save user:

public function store()
{

    $this->validate(request(),[
        'name'=>'required',
        'email'=>'required',
        'password'=>'required'
    ]);

   $user = User::create(request(['name', 'email','password']));

    auth()->login($user);

    return redirect()->home();
}

      

Everything works fine now, but when I go to the login form, I cannot login. Here's my SessionController that deals with login:

public function store()
{

if(!auth()->attempt(request(['email','password']))) 

    return back();
}
 return redirect()->home();
}

      

What am I doing wrong here that I cannot login ?!

+3


source to share


1 answer


In this case, I see that when you register a user, you are storing your password as PLAIN PHONE in the database, which is VERY WRONG.

Now the problem comes when attempt()

the user tries to log in, he receives an email and the password will encrypt the password to compare with the hashed in the database (your u case is saved as plain text).



When you create user bcrypt

() the password is like this:

public function store()
    {

        $this->validate(request(),[
            'name'=>'required',
            'email'=>'required',
            'password'=>'required'
        ]);


       $user = User::create([ 
            'name' => request('name'),
            'email' => request('email'),
            'password' => bcrypt(request('password'))
            ]);


        auth()->login($user);

        return redirect()->home();
    }

      

+8


source







All Articles