Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User can not login after registering on Laravel

Tags:

php

laravel

I am trying to set up authentication system on Laravel from scratch but I can not make the user to log in after the user its registered.

My RegisterController to store the 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();
}

Now everything works great but when i go to login form I cant log in. Here is my SessionsController that deals with login:

public function store()
{

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

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

What I am doing wrong here that I can not log in the user?!

like image 995
David Larsonn Avatar asked Dec 23 '22 16:12

David Larsonn


1 Answers

In this case I see that when you register a user you are saving their password as a PLAIN TEXT on database, which is VERY WRONG.

Now the issue happens when attempt() is trying to login the user, it is getting the email and password will bcrypt the password to compare with a hashed one on database(your case u are saving as a plain text).

When you create the user bcrypt() the password like so:

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();
    }
like image 93
Leo Avatar answered Mar 01 '23 23:03

Leo