Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
2.3 kB
1
Indexable
Never
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
if(isset($_GET['code']) && isset($token['error'])) {

    $facebook_helper = $facebook->getRedirectLoginHelper();
    if (isset($_SESSION['access_token'])) {
        $access_token = $_SESSION['access_token'];
    } else {
        $access_token = $facebook_helper->getAccessToken();
        $_SESSION['access_token'] = $access_token;
        $facebook->setDefaultAccessToken($_SESSION['access_token']);
    }

    $graph_response = $facebook->get("/me?fields=name,email", $access_token);
    $facebook_user_info = $graph_response->getGraphUser();

    if (!empty($facebook_user_info['id'])) {
        $_SESSION['user_image'] = 'http://graph.facebook.com/' . $facebook_user_info['id'] . '/picture';
    }
    if (!empty($facebook_user_info['id'])) {
        $_SESSION['user_id'] = $facebook_user_info['id'];
    }
    if (!empty($facebook_user_info['name'])) {
        $_SESSION['user_name'] = $facebook_user_info['name'];
    }
    if (!empty($facebook_user_info['email'])) {
        $_SESSION['user_email_address'] = $facebook_user_info['email'];
    }
}
elseif(isset($_GET['code']) && !isset($token['error'])) {
    $google_client->setAccessToken($token['access_token']);

    //Store "access_token" value in $_SESSION variable for future use.
    $_SESSION['access_token'] = $token['access_token'];

    //Create Object of Google Service OAuth 2 class
    $google_service = new Google_Service_Oauth2($google_client);

    //Get user profile data from google
    $data = $google_service->userinfo->get();
    if (!empty($data['sub'])) {
        $_SESSION['user_id'] = $data['sub'];
    }
    if (!empty($data['given_name'])) {
        $_SESSION['user_name'] = $data['given_name'] . " " . $data['family_name'];
    }
    if (!empty($data['email'])) {
        $_SESSION['user_email_address'] = $data['email'];
    }
    if (!empty($data['picture'])) {
        $_SESSION['user_image'] = $data['picture'];
    }
}
else {
    $facebook_helper = $facebook->getRedirectLoginHelper();
    // Get login url
    $facebook_permissions = ['email']; // Optional permissions
    $facebook_login_url = $facebook_helper->getLoginUrl('https://2goe.com/demo/'.$lang.'/home/', $facebook_permissions);
}