<?php
require 'db.php';
require 'helper.php';
if(isset($_POST["send"])){
    $data = $_POST["list"];
    login($data);
}

function login($data) {
    global $conn;
    
    $username = $data[0];
    $password = $data[1];
    
    $stmt = $conn->prepare("SELECT * FROM users WHERE username = :username");
    $stmt->bindParam(':username', $username);
    $stmt->execute();
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($user && password_verify($password, $user['password'])) {
        $token = generateToken();
        
        $stmt = $conn->prepare("UPDATE users SET token = :token WHERE id = :id");
        $stmt->bindParam(':token', $token);
        $stmt->bindParam(':id', $user['id']);
        $stmt->execute();
        session_start();
		$_SESSION['username']=$token;
        echo  $token;
    } else {
        echo 'Login failed';
    }
}