📄 Source: new_chat.php
<?php
session_start();
require_once 'database.php';
header('Content-Type: application/json');
if (!isset($_SESSION['access_token'])) {
echo json_encode(['error' => 'Not authenticated']);
exit();
}
// Get user email
if (!isset($_SESSION['user_email']) && isset($_SESSION['access_token'])) {
try {
$tempClient = new Google\Client();
$tempClient->setAccessToken($_SESSION['access_token']);
$oauth2 = new Google\Service\Oauth2($tempClient);
$userInfo = $oauth2->userinfo->get();
$_SESSION['user_email'] = $userInfo->email;
} catch (Exception $e) {
$_SESSION['user_email'] = 'user_' . session_id();
}
}
$userEmail = $_SESSION['user_email'] ?? 'guest_' . session_id();
$db = new ChatDatabase();
$newChatId = $db->createConversation($userEmail);
$_SESSION['current_conversation_id'] = $newChatId;
echo json_encode(['success' => true, 'chat_id' => $newChatId]);
?>
← Back