简体   繁体   中英

User can not login after registering on 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?!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM