public function checkSession()
	{
		// Get session token
		$session_token = isset($_COOKIE[$this->cookie_name]) ? $_COOKIE[$this->cookie_name]
			: (isset($_SERVER['HTTP_AUTH_TOKEN']) ? $_SERVER['HTTP_AUTH_TOKEN'] : null);

		// Get reference to session state machine
		if ($session_token) {
			// Get session machine ref
			$session_id = (array) $this->session_machine_ref_prefix;
			$session_id[] = $session_token;
			$this->session_machine = $this->smalldb->ref($session_id);

			// Check session
			if ($this->session_machine->state == '') {
				// Invalid token
				$this->session_machine = $this->smalldb->ref($this->session_machine_null_ref);
			} else {
				// TODO: Validate token, not only session existence, and check whether session is in valid state (invoke a transition to check).

				// TODO: Session should tell how long it will last so the cookie should have the same duration.

				// Token is good - refresh cookie
				setcookie($this->cookie_name, $session_token, time() + $this->cookie_ttl, '/', null, !empty($_SERVER['HTTPS']), true);
			}
		} else {
			// No token
			$this->session_machine = $this->smalldb->ref($this->session_machine_null_ref);
		}

		// Update token on state change
		$t = $this;
		$this->session_machine->afterTransition()->addListener(function($ref, $transition_name, $arguments, $return_value, $returns) use ($t) {
			if (!$ref->state) {
				// Remove cookie when session is terminated
				setcookie($t->cookie_name, null, time() + $t->cookie_ttl, '/', null, !empty($_SERVER['HTTPS']), true);
				if (isset($_SESSION)) {
					// Kill PHP session if in use
					session_regenerate_id(true);
				}
			} else {
				// Update cookie on state change
				setcookie($t->cookie_name, $ref->id, time() + $t->cookie_ttl, '/', null, !empty($_SERVER['HTTPS']), true);
			}
		});

		// Everything else is up to the state machine
	}