Beispiel #1
0
require_once __DIR__ . '/../lib/google-api-php-client/src/Google/autoload.php';
require_once __DIR__ . '/mysql.php';
header('Content-Type: application/json; charset=utf-8');
ob_start(NULL, 0, PHP_OUTPUT_HANDLER_CLEANABLE);
try {
    $client = new Google_Client();
    $client->setAuthConfigFile('client_secrets.json');
    $client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
    $client->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
    // Fetch the user
    $token = htmlspecialchars_decode($_POST['id_token']);
    $id = get_user_id_from_token($client, $token);
    update_user_login_time($id, ID_TYPE_GOOGLE);
    $user = get_user($id, ID_TYPE_GOOGLE);
    $data = get_user_data_from_token($client, $token);
    // If the user doesn't exist, create a new one
    if (isset($user->error)) {
        throw new Exception($user->error);
    } else {
        if (empty($user)) {
            $user = create_user($client, $token);
        }
    }
    // Include current picture in user data
    if (isset($data['payload']['picture'])) {
        $user['picture'] = $data['payload']['picture'];
    } else {
        $user['picture'] = null;
    }
    // Include current locale in user data
Beispiel #2
0
/**
 * Creates a new user using the information in the given token and returns the result.
 */
function create_user($client, $token)
{
    $google_id = get_user_id_from_token($client, $token);
    $data = get_user_data_from_token($client, $token);
    $link = db_connect();
    $google_id = mysqli_real_escape_string($link, $google_id);
    $name = mysqli_real_escape_string($link, $data['payload']['name']);
    $email = mysqli_real_escape_string($link, $data['payload']['email']);
    $query = "INSERT INTO users (id, google_id, email, name, last_login, created_at, updated_at) VALUES (NULL, '{$google_id}', '{$email}', '{$name}', NULL, NULL, NULL);";
    $result = mysqli_query($link, $query);
    mysqli_close($link);
    return get_user($google_id, ID_TYPE_GOOGLE);
}