<?php

include __DIR__.'/account.php';

class Login extends Vn\Web\JsonRequest {
	function run($db) {
		if (!$_POST['user'] || !$_POST['password'])
			throw new Vn\Web\BadLoginException();

		try {
			Account::trySync($db
				,strtolower($_POST['user'])
				,$_POST['password']
			);
		} catch (Exception $e) {
			error_log($e->getMessage());
		}

		$token = $this->service->createToken(
			$_SESSION['user'],
			!empty($_POST['remember'])
		);

		return [
			'login' => TRUE,
			'token' => $token
		];
	}
}

