feat: check for password existence

This commit is contained in:
insects 2025-03-11 01:12:30 +01:00
parent b03df8f0e4
commit 498ecdf68b
7 changed files with 73 additions and 15 deletions

View file

@ -84,7 +84,7 @@ small.badge {
border-color: #63f0fd; border-color: #63f0fd;
} }
button.action { .action button {
width: 100%; width: 100%;
height: 100%; height: 100%;
border: 0; border: 0;
@ -103,6 +103,15 @@ button.action:hover {
cursor: pointer; cursor: pointer;
} }
.action {
display: none;
}
.action.shown {
display: block;
height: 100%;
}
section .meta { section .meta {
padding-left: 10px; padding-left: 10px;
padding-top: 10px; padding-top: 10px;

View file

@ -6,7 +6,9 @@ class InstanceController < ApplicationController
password = Nanoid.generate(size: 3, alphabet: "0123456789") password = Nanoid.generate(size: 3, alphabet: "0123456789")
instance = Instance.new(zone: zone, public_id: public_id, name: name, password: password) instance = Instance.new(zone: zone, public_id: public_id, name: name, password: password)
if instance.save if instance.save
redirect_to(show_instance_path(public_id: public_id)) @id = instance.public_id
@password = instance.password
render "set_password"
end end
end end
@ -15,22 +17,26 @@ class InstanceController < ApplicationController
end end
def pop def pop
instance_id, nm = pop_instance_params instance_id, nm, pwd = pop_instance_params
parent_instance = Instance.find_by(public_id: instance_id) parent_instance = Instance.find_by(public_id: instance_id)
if parent_instance.password == pwd
pop = Pop.new(instance_id: parent_instance.id, name: nm) pop = Pop.new(instance_id: parent_instance.id, name: nm)
if pop.save if pop.save
@instance = Instance.includes(:pops).find_by(public_id: instance_id) @instance = Instance.includes(:pops).find_by(public_id: instance_id)
render partial: "list", locals: { instance: @instance } render partial: "list", locals: { instance: @instance }
end end
end end
end
def reset def reset
instance_id, nm = pop_instance_params instance_id, nm, pwd = pop_instance_params
parent_instance = Instance.find_by(public_id: instance_id) parent_instance = Instance.find_by(public_id: instance_id)
if parent_instance.password == pwd
Pop.delete_by(instance_id: parent_instance.id, name: nm) Pop.delete_by(instance_id: parent_instance.id, name: nm)
@instance = Instance.includes(:pops).find_by(public_id: instance_id) @instance = Instance.includes(:pops).find_by(public_id: instance_id)
render partial: "list", locals: { instance: @instance } render partial: "list", locals: { instance: @instance }
end end
end
private private
@ -43,6 +49,6 @@ class InstanceController < ApplicationController
end end
def pop_instance_params def pop_instance_params
params.expect(:instance, :nm) params.expect(:instance, :nm, :pwd)
end end
end end

22
app/javascript/list.js Normal file
View file

@ -0,0 +1,22 @@
function checkPwd() {
const ls = window.localStorage;
const id = document.getElementById("public_id").dataset.content;
const pwd = ls.getItem(`ecoffee-pwd:${id}`);
if (pwd) {
const to_show = document.querySelectorAll(".needs_pwd");
to_show.forEach(el => {
el.classList.add("shown");
});
const buttons = document.querySelectorAll(".action button");
buttons.forEach(btn => {
const oldUrl= btn.getAttribute("hx-post");
btn.setAttribute("hx-post", `${oldUrl}&pwd=${pwd}`);
});
}
}
checkPwd();
document.addEventListener("htmx:afterRequest", evt => {
checkPwd();
})

View file

@ -0,0 +1,12 @@
const id = document.getElementById("public_id");
const pwd = document.getElementById("password");
const ls = window.localStorage;
if (id && pwd) {
ls.setItem(`ecoffee-pwd:${id.dataset.content}`, pwd.dataset.content);
}
let loc = new URL(window.location.href);
loc.search = "";
loc.pathname = `/${id.dataset.content}`;
window.location = loc;

View file

@ -35,7 +35,7 @@
</span> </span>
<% end %> <% end %>
</div> </div>
<div class="button"> <div class="action needs_pwd">
<% if is_popped %> <% if is_popped %>
<button <button
class="action reset" class="action reset"
@ -49,7 +49,6 @@
hx-target="#nm-list" hx-target="#nm-list"
>Pop</button> >Pop</button>
<% end %> <% end %>
</div> </div>
</section> </section>
<% end %> <% end %>

View file

@ -0,0 +1,6 @@
<h1>Redirecting...</h1>
<div id="public_id" data-content="<%= @id %>"></div>
<div id="password" data-content="<%= @password %>"></div>
<%= javascript_include_tag "save_password" %>

View file

@ -1,9 +1,13 @@
<div id="public_id" data-content="<%= @instance.public_id %>"></div>
<div hx-get="" hx-trigger="every 30s" hx-swap="innerHTML" hx-target="this"> <div hx-get="" hx-trigger="every 30s" hx-swap="innerHTML" hx-target="this">
<header> <header>
<img src="/icon.png" width="50" alt="eureka.coffee logo" /> <%= link_to root_path do %><img src="/icon.png" width="50" alt="eureka.coffee logo" /><% end %>
<h1><span class="muted">instance</span> <%= @instance.name %></h1> <h1><span class="muted">instance</span> <%= @instance.name %></h1>
<%= render partial: "zone_img", locals: { zone: @instance.zone, alt: @instance.zone, title: @instance.zone.upcase_first } %> <%= render partial: "zone_img", locals: { zone: @instance.zone, alt: @instance.zone, title: @instance.zone.upcase_first } %>
</header> </header>
<%= render partial: "list", locals: { instance: @instance } %> <%= render partial: "list", locals: { instance: @instance } %>
<%= javascript_include_tag "list" %>
</div> </div>