<?php

return [
  "pattern" => "login.json",
  "method" => "POST",
  "action"  => function () {
    $json = file_get_contents("php://input");
    $data = json_decode($json);

    $kirby = kirby();

    $email = $data->email;
		$password = $data->password;
		
    if(V::email($email)) {
			try {
        $kirby->auth()->login($email, $password, true);
        return json_encode([
          "status" => "success",
          "role" => (string) $kirby->user()->role()
        ]);
      } catch (Exception $e) {
        return json_encode([
          "status" => "error",
          "message" => "<strong>Email ou mot de passe invalide.</strong><br>Contactez l'administrateur pour demander la réinitialisation de vos informations de connexion."
        ]);
      }
		} else {
      return json_encode([
        "status" => "error",
        "message" => "<strong>Email invalide.</strong>"
      ]);
    }
  },
];
