<?php
//uzsāk jaunu sesiju
session_start();
// konekcijas piesaiste
include('connection.php');

//saglabā e-pastu un paroli mainigajos
$email = $_POST['email'];
$password = $_POST['password'];

//izgīust visu info par profilu no datubāzes
$sql_check = "SELECT * FROM rr_410_profile WHERE email = '$email'";// vaicājums
$result = $conn->query($sql_check);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $stored_hashed_password = $row['password']; //izgūst šifrēto paroli
    
    //pārbaiuda šīfrēto aproli
    if (password_verify($password, $stored_hashed_password)) {
        $_SESSION['profile_id'] = $row['profile_id'];//pievieno sesijai profila ID
        $_SESSION['email'] = $email; //pievieno sesijai e-pasta adresi
        header("Location: save_profile_id.php");
        exit();
    } else {
        echo "<script> alert('Invalid password. Please try again.'); location.href='../html/login.html'; </script>";//kļūdas ziņojums
    }
} else {
    echo "<script>alert('Incorect or not registered e-mail.'); location.href='../html/login.html';</script>";//kļūdas ziņojums
}
// aizvert konekciju
$conn->close();
?>
