Question

I have an authentication system for Facebook login in Laravel that works fine when a user with all of the properties logs in, but when a user doesn't have one of the properties filled out, it throws an error (for example):

Undefined property: stdClass::$bio

Here is the relevant code:

Facebook.php:

public function get_user_info(Token_Access $token)
    {
        $url = 'https://graph.facebook.com/me?'.http_build_query(array(
            'access_token' => $token->access_token,
        ));

        $user = json_decode(file_get_contents($url));

        // Create a response from the request
        return array(
            'uid' => $user->id,
            'nickname' => $user->username,
            'name' => $user->name,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'gender' => $user->gender,
            'email' => $user->email,
            'birthday' => $user->birthday,
            'location' => $user->location->name,
            'description' => $user->bio, //the line giving trouble for a user
            'friends'  => 'https://graph.facebook.com/me/friends?fields=id&access_token='.$token->access_token,
            'image' => 'https://graph.facebook.com/me/picture?width=200&height=200&access_token='.$token->access_token,
            'urls' => array(
            'Facebook' => $user->link,
            ),
        );
    }

Oauth2Controller.php:

if(is_null($check)) {

                  $fan = new Fan;

                  $fan->fbid = $user['uid'];
                  $fan->email = $user['email'];
                  $fan->first_name = $user['first_name'];
                  $fan->last_name = $user['last_name'];
                  $fan->gender = $user['gender'];
                  $fan->birthday = $user['birthday'];
                  $fan->age = $age;
                  $fan->city = $city;
                  $fan->state = $state_abbrev;
                  $fan->image = $user['image'];
                  $fan->friend_ids_url = $user['friends'];
                  $fan->friend_ids_unpacked = $friend_ids;

                  $fan->save();

                  $new = Fan::where('fbid', '=', $user['uid'])->first();

                  Auth::login($new);
                  return Redirect::to('fans/home');

                }

How do I check that a user has these fields included with their profile, so I don't have this error get thrown? Thank you.

Was it helpful?

Solution

Can you do this?

 'description' => (isset($user->bio) ? $user->bio : NULL)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top