if not session_get("logged_in") then
	local cookie = cookie_get("dkauth")

	if cookie then
		local db = sqlite("dk")
		local d = db:get_one("SELECT * FROM user_sessions WHERE token = :t", {t = cookie})

		if d then
			if d.expires < os.time() then
				db:exec("DELETE FROM user_sessions WHERE token = :t", {t = cookie})
				session_set("logged_in", false)
			else
				session_set("logged_in", true)
				session_set("user_id", d.user_id)
			end
		else
			session_set("logged_in", false)
		end
	end
end