diff --git a/apps/fg_http/assets/js/hooks/hooks.js b/apps/fg_http/assets/js/hooks/hooks.js index 96fdafdde..835c8b468 100644 --- a/apps/fg_http/assets/js/hooks/hooks.js +++ b/apps/fg_http/assets/js/hooks/hooks.js @@ -3,6 +3,9 @@ import { qrEncode } from "./qr_code.js" let Hooks = {} Hooks.QrEncode = { + mounted() { + qrEncode() + }, updated() { qrEncode() } diff --git a/apps/fg_http/lib/fg_http/devices.ex b/apps/fg_http/lib/fg_http/devices.ex index eef55d84a..b1638d20e 100644 --- a/apps/fg_http/lib/fg_http/devices.ex +++ b/apps/fg_http/lib/fg_http/devices.ex @@ -90,6 +90,19 @@ defmodule FgHttp.Devices do |> Repo.update() end + @doc """ + Verifies a Device. + + ## Examples + + iex> verify!(device.id) + {:ok, %Device{}} + """ + def verify(id) do + get_device!(id) + |> update_device(%{verified_at: DateTime.utc_now}) + end + @doc """ Deletes a device. diff --git a/apps/fg_http/lib/fg_http/devices/device.ex b/apps/fg_http/lib/fg_http/devices/device.ex index e37f4d936..57706dbc3 100644 --- a/apps/fg_http/lib/fg_http/devices/device.ex +++ b/apps/fg_http/lib/fg_http/devices/device.ex @@ -20,7 +20,7 @@ defmodule FgHttp.Devices.Device do @doc false def changeset(device, attrs) do device - |> cast(attrs, [:name, :public_key]) - |> validate_required([:name]) + |> cast(attrs, [:user_id, :verified_at, :name, :public_key]) + |> validate_required([:user_id]) end end diff --git a/apps/fg_http/lib/fg_http/users/user.ex b/apps/fg_http/lib/fg_http/users/user.ex index 5dbddfe94..c4fe12691 100644 --- a/apps/fg_http/lib/fg_http/users/user.ex +++ b/apps/fg_http/lib/fg_http/users/user.ex @@ -19,7 +19,7 @@ defmodule FgHttp.Users.User do def changeset(user, attrs \\ %{}) do user |> cast(attrs, [:email, :confirmed_at, :password_digest, :last_signed_in_at]) - |> validate_required([:email, :last_signed_in_at]) + |> validate_required([:email]) |> unique_constraint(:email) end end diff --git a/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex b/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex index 3763e3444..3dd2106eb 100644 --- a/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex +++ b/apps/fg_http/lib/fg_http_web/controllers/device_controller.ex @@ -18,6 +18,25 @@ defmodule FgHttpWeb.DeviceController do render(conn, "new.html", changeset: changeset) end + def create(conn, %{"device" => device_params}) do + create_params = %{ + "user_id" => conn.assigns.current_user.id, + "verified_at" => DateTime.utc_now, + "name" => "Auto" + } + all_params = Map.merge(device_params, create_params) + IO.puts "Create device with params: " + IO.inspect all_params + case Devices.create_device(all_params) do + {:ok, device} -> + IO.inspect(device) + redirect(conn, to: Routes.device_path(conn, :show, device)) + {:error, %Ecto.Changeset{} = changeset} -> + IO.inspect(changeset) + render(conn, "new.html", changeset: changeset) + end + end + def show(conn, %{"id" => id}) do device = Devices.get_device!(id) render(conn, "show.html", device: device) diff --git a/apps/fg_http/lib/fg_http_web/live/new_device_live.ex b/apps/fg_http/lib/fg_http_web/live/new_device_live.ex index 5e59f69be..5e41759a6 100644 --- a/apps/fg_http/lib/fg_http_web/live/new_device_live.ex +++ b/apps/fg_http/lib/fg_http_web/live/new_device_live.ex @@ -1,19 +1,23 @@ defmodule FgHttpWeb.NewDeviceLive do use Phoenix.LiveView + use Phoenix.HTML + alias FgHttpWeb.Router.Helpers, as: Routes alias FgHttp.Devices.Device - def mount(_params, %{"current_user_id" => user_id}, socket) do + def mount(_params, %{}, socket) do + user_id = "1" + IO.inspect(socket) if connected?(socket), do: wait_for_device(socket) - device = %Device{user_id: user_id} + device = %Device{id: "1", user_id: user_id} {:ok, assign(socket, :device, device)} end defp wait_for_device(socket) do # TODO: pass socket to fg_vpn somehow IO.inspect(socket) - :timer.send_after(10000, self(), :update) + :timer.send_after(3000, self(), :update) end def handle_info(:update, socket) do diff --git a/apps/fg_http/lib/fg_http_web/live/new_device_live.html.leex b/apps/fg_http/lib/fg_http_web/live/new_device_live.html.leex index a36e33766..1bdcfd0a9 100644 --- a/apps/fg_http/lib/fg_http_web/live/new_device_live.html.leex +++ b/apps/fg_http/lib/fg_http_web/live/new_device_live.html.leex @@ -1,5 +1,5 @@
- Add the following to your Wireguard configuration file: + Add the following to your WireGuard™ configuration file:
- When we receive a connection from your device, we'll prompt you verify it here. -
- -- Waiting for device connection... - - <%= if @device.public_key do %> - Connected! Device Public Key: <%= @device.public_key %> - Configure your device -> - <% end %> - -
++ When we receive a connection from your device, we'll prompt you verify it here. +
+ <% else %> ++ Device Public Key: <%= @device.public_key %> +
++ <%# XXX: Use the public key sent by the actual device and not the one here %> + <%= + link("This looks good, verify and continue. ->", + to: Routes.device_path(@socket, :create, "device[public_key]": @device.public_key), + method: :post) + %> +
++ <%= + link("<- Something's wrong. Don't add this device and go back.", + to: Routes.device_path(@socket, :index), + method: :delete) + %> +
+ <% end %> +<%= link "Back", to: Routes.device_path(@conn, :index) %>