samedi 7 janvier 2017

Laravel 5.3 facebook Login. Not logging in a new user at the first time

I am using socialite to integrate the login with Facebook function. Here is my code:

public function handleProviderCallback()
{

    try {
        $socialUser = Socialite::driver('facebook')->user();
    } catch (\Exception $e) {
        return redirect('/dashboard');
    }


    /*section1 : i check if the user exists in the db, and if no then 
    I save this user in the db */

    $user = User::where('facebook_id', $socialUser->getId())->first();
    if (!$user) {
        User::create([
            'facebook_id' => $socialUser->getId(),
            'name' => $socialUser->getName(),
            'email' => $socialUser->getEmail(),
        ]);

        Auth::loginUsingId($socialUser->id);
        return redirect()->intended('/dashboard');
        }

        /* end of section one */


        else{
        if(Auth::loginUsingId($user->id)){
            return redirect()->intended('/dashboard');
        }
    }


}

The problem is: I login with a new user, the data is added to the database and I am redirected to the dashboard, but I am not logged in. After I try logging in again (when the user already exists in the database) I am successfully logged in. Why? Thanks!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ioZ9Hc
via IFTTT

Aucun commentaire:

Enregistrer un commentaire