<?php
session_start();
require_once 'config.php';

$is_authenticated = false;

if (isset($_GET['token']) && isset($_GET['expires']) && isset($_GET['signature'])) {
    $user_data = validateFormoreToken($_GET['token'], $_GET['expires'], $_GET['signature']);
    
    if ($user_data) {
        storeUserSession($user_data);
        $is_authenticated = true;
        
        header('Location: /home.php');
        exit;
    } else {
        redirectToLogin();
    }
} else {
    $is_authenticated = checkExistingSession();
}

if (!$is_authenticated) {
    redirectToLogin();
}

$current_user = [
    'id' => $_SESSION['formore_user_id'],
    'email' => $_SESSION['formore_email'],
    'username' => $_SESSION['formore_username'],
    'profile_picture' => $_SESSION['formore_profile_picture']
];
?>