vendredi 28 juillet 2017

laravel socialite get user informations

I can get user's informations name, gender, etc, but when i try to access user's birthday login doesn't work, this is my code for LoginController

public function redirectToProvider()
    {
        return Socialite::driver('facebook')->fields([
            'first_name', 'last_name', 'email', 'gender', 'birthday'
        ])->scopes([
            'email', 'user_birthday'
        ])->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        try
        {
            $facebook_user = Socialite::driver('facebook')->fields([
                'first_name', 'last_name', 'email', 'gender', 'birthday'
            ])->scopes([
                'email', 'user_birthday'
            ])->user();
            // echo $facebook_user->getAvatar();
// this returns the data but when i try to access
// $facebook_user->user['birthday'] it doesnt give any error or login
            // dd($facebook_user->user['birthday']);
            $facebook_user_id = $facebook_user->getId(); // unique facebook user id
            $user = User::where('facebook_user_id', $facebook_user_id)->first();
            // dd($user);
            if (!$user)
            {
                // dd($facebook_user->avatar);
                $user = new User;
                $user->facebook_user_id = $facebook_user_id;
                $user->name = $facebook_user->getName();
                $user->avatar = $facebook_user->getAvatar();
                $user->email = $facebook_user->getEmail();
                $user->gender = $facebook_user->user['gender'];

                // $user->birthday = $facebook_user->user['birthday'];
                // dd($user);
                $user->save();
                auth()->login($user);
                return redirect('/location');
            }
            else
            auth()->login($user);
        }
        return redirect('/location');
    }

and I want to access more informations like users hometown, i added fields and scopes but it doesn't give any error, just returns to login page



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

Aucun commentaire:

Enregistrer un commentaire