[ 'client_id' => 'YOUR_GOOGLE_CLIENT_ID', 'client_secret' => 'YOUR_GOOGLE_CLIENT_SECRET', 'token_url' => 'https://oauth2.googleapis.com/token', 'userinfo_url' => 'https://www.googleapis.com/oauth2/v2/userinfo', 'redirect_uri' => URL::Link('auth/callback') ] ]; // Check for OAuth errors if ($error) { $error_message = $error_description ?: $error; URL::$fragments['error'] = "OAuth Error: " . htmlspecialchars($error_message); URL::$fragments['error_type'] = 'oauth_error'; } else if ($code && $state) { // Determine which OAuth provider this is for (you'd need to store this during the auth initiation) $service = $_SESSION['oauth_service'] ?? 'google'; if (!isset($oauth_config[$service])) { URL::$fragments['error'] = "Unknown OAuth service: $service"; URL::$fragments['error_type'] = 'invalid_service'; } else { $config = $oauth_config[$service]; // Check if configuration is complete if ($config['client_id'] === 'YOUR_GOOGLE_CLIENT_ID' || $config['client_secret'] === 'YOUR_GOOGLE_CLIENT_SECRET') { URL::$fragments['demo_mode'] = true; URL::$fragments['success'] = "OAuth callback received successfully! (Demo Mode)"; URL::$fragments['code'] = substr($code, 0, 20) . '...'; URL::$fragments['state'] = $state; URL::$fragments['service'] = $service; } else { // STEP 1: Exchange authorization code for access token $token_response = HTTP::post($config['token_url'], [ 'client_id' => $config['client_id'], 'client_secret' => $config['client_secret'], 'code' => $code, 'grant_type' => 'authorization_code', 'redirect_uri' => $config['redirect_uri'] ]); if (!$token_response['success']) { URL::$fragments['error'] = "Token exchange failed: " . ($token_response['error'] ?? 'Unknown error'); URL::$fragments['error_type'] = 'token_exchange_failed'; URL::$fragments['debug'] = $token_response; } else { $tokens = $token_response['data']; $access_token = $tokens['access_token']; // STEP 2: Get user profile information $profile_response = HTTP::get_with_token($config['userinfo_url'], $access_token); if (!$profile_response['success']) { URL::$fragments['error'] = "Failed to get user profile: " . ($profile_response['error'] ?? 'Unknown error'); URL::$fragments['error_type'] = 'profile_fetch_failed'; URL::$fragments['debug'] = $profile_response; } else { $profile = $profile_response['data']; // STEP 3: Create or login user // This is where you'd typically: // 1. Check if user exists by email // 2. Create new user if doesn't exist // 3. Update user profile with OAuth data // 4. Set session variables // For demo purposes, just set session data $_SESSION['user_id'] = $profile['id']; $_SESSION['user_email'] = $profile['email']; $_SESSION['user_name'] = $profile['name']; $_SESSION['user_picture'] = $profile['picture'] ?? null; $_SESSION['oauth_provider'] = $service; $_SESSION['logged_in_at'] = time(); URL::$fragments['success'] = "Successfully logged in with " . ucfirst($service) . "!"; URL::$fragments['user_profile'] = $profile; URL::$fragments['redirect_to'] = 'dashboard'; // Where to redirect after success } } } } // Clean up temporary OAuth session data unset($_SESSION['oauth_service']); unset($_SESSION['oauth_state']); } else { URL::$fragments['error'] = "Invalid OAuth callback - missing required parameters"; URL::$fragments['error_type'] = 'invalid_callback'; } ?>
= safe(URL::$fragments['success']) ?>
Service: = safe(URL::$fragments['service']) ?>
Code: = safe(URL::$fragments['code']) ?>
State: = safe(URL::$fragments['state']) ?>
Replace 'YOUR_GOOGLE_CLIENT_ID' and 'YOUR_GOOGLE_CLIENT_SECRET' with actual values
= htmlspecialchars('// Exchange code for token
$token_response = HTTP::post("https://oauth2.googleapis.com/token", [
"client_id" => $client_id,
"client_secret" => $client_secret,
"code" => $code,
"grant_type" => "authorization_code",
"redirect_uri" => $redirect_uri
]);
// Get user profile
$profile_response = HTTP::get_with_token(
"https://www.googleapis.com/oauth2/v2/userinfo",
$token_response["data"]["access_token"]
);
// Login/create user
$profile = $profile_response["data"];
$user = User::FindByEmail($profile["email"]) ?: User::Create([
"email" => $profile["email"],
"name" => $profile["name"],
"picture" => $profile["picture"],
"oauth_provider" => "google",
"oauth_id" => $profile["id"]
]);
// Set session
$_SESSION["user_id"] = $user["id"];
$_SESSION["user_email"] = $user["email"];
$_SESSION["logged_in_at"] = time();
// Redirect to dashboard
URL::Redirect("dashboard");') ?>
= htmlspecialchars(json_encode(URL::$fragments['debug'], JSON_PRETTY_PRINT)) ?>