简体   繁体   中英

Register & Authentication Laravel 5.2

Good afternoon !

I am creating and authenticating with Laravel 5.2 and I have the following problem , I am using Socialite with google for this case , the user could be register correctly but in the authenticacion with this code fails.

Could anyone help to me ?

 public function handleProviderCallback(){
        $user = Socialite::driver('google')->user();
        $usuario = new User();
        $usuario->name=$user->name;
        $usuario->email=$user->email;
        $usuario->password=\Hash::make("1234");
        $usuario->photo=$user->avatar;
        $usuario->role_id=1;
        $usuario->isLocked=0;
        $usuario->save();       
        $findUser= User::where('email','=',$usuario->email)->get();
        if (!$buscarUsuario->isEmpty()) { 
            if (\Auth::attempt(['email' => $findUser[0]->email, 'password' => $findUser[0]->password]))
            {
                 return redirect()->intended('profile');
            }else{
                echo "NO";
                //return \Redirect::back();      
            }

        }else{
            echo "NOTHING";
        }
    }

The result doing echo in $buscarUsuario

[{"id":81,"name":"Kiko","first_surname":"","last_surname":"","descripcion":"","email":"correo@gmail.com","created_at":"2016-06-04 14:15:07","updated_at":"2016-06-04 14:15:07"}

Use

Auth::loginUsingId($findUser['id']); 

https://laravel.com/docs/5.1/authentication#other-authentication-methods

method for login form ID

The issue is that when you call Auth::attempt() , it is expecting the plaintext password, but you are passing the hashed password. This is why it fails.

Since you have selected the User object and have the ID, you can call \\Auth::loginUsingId() like below.

Also, you are calling isEmpty() on $buscarUsuario , which doesn't appear to exist. That should be $findUser .

if(!$findUser->isEmpty()){
    if (\Auth::loginUsingId($findUser[0]->id))
    {
         return redirect()->intended('profile');
    }else{
        echo "NO";
        //return \Redirect::back();      
    }
 } else {
     echo "NO";
 }

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