Site Builder
Editing:
google-callback.php
writable 0666
<?php require_once __DIR__.'/../lib/db.php'; session_start(); // 1. state check if (!isset($_GET['state'], $_SESSION['oauth2state']) || $_GET['state'] !== $_SESSION['oauth2state']) { exit('Bad state'); } unset($_SESSION['oauth2state']); // 2. exchange code for tokens $resp = file_get_contents('https://oauth2.googleapis.com/token', false, stream_context_create(['http' => [ 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query([ 'code' => $_GET['code'], 'client_id' => GOOGLE_CLIENT_ID, 'client_secret' => GOOGLE_CLIENT_SECRET, 'redirect_uri' => GOOGLE_REDIRECT_URI, 'grant_type' => 'authorization_code' ]) ]])); $tok = json_decode($resp, true); if (!isset($tok['id_token'])) exit('No id_token'); // 3. decode ID token (header.payload.signature – we trust Google HTTPS) $parts = explode('.', $tok['id_token']); $payload = json_decode(base64_decode(strtr($parts[1], '-_', '+/')), true); $sub = $payload['sub']; // Google unique ID $email = $payload['email']; $name = $payload['name'] ?? ''; // 4. find or create user $stmt = $db->prepare('SELECT id, site_slug, acct_type FROM users WHERE google_sub=? OR email=?'); $stmt->execute([$sub, $email]); $u = $stmt->fetch(); if ($u) { // ‑‑> existing account if (!$u['google_sub']) { // link if first time $db->prepare('UPDATE users SET google_sub=?, google_name=? WHERE id=?') ->execute([$sub, $name, $u['id']]); } $_SESSION['uid'] = $u['id']; header('Location: /members/dashboard.php'); exit; } /* 5. first‑time Google user: stash info → redirect to choose‑slug */ $_SESSION['g_pending'] = ['sub'=>$sub,'email'=>$email,'name'=>$name]; header('Location: /members/oauth/choose-slug.php'); exit;
Save changes
Create folder
writable 0777
Create
Cancel