-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PETAL #81
base: master
Are you sure you want to change the base?
PETAL #81
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
import '../../deps/phoenix_html/priv/static/phoenix_html.js' | ||
import { Socket } from 'phoenix' | ||
import LiveSocket from 'phoenix_live_view' | ||
import topbar from 'topbar' | ||
|
||
import '../styles/main.scss' | ||
import 'alpinejs' | ||
import 'phoenix_html/priv/static/phoenix_html.js' | ||
|
||
function documentReady (fn) { | ||
if (document.readyState === 'complete' || document.readyState === 'interactive') { | ||
setTimeout(fn, 1) | ||
} else { | ||
document.addEventListener('DOMContentLoaded', fn) | ||
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content') | ||
const liveSocket = new LiveSocket('/live', Socket, { | ||
params: { | ||
_csrf_token: csrfToken | ||
} | ||
} | ||
}) | ||
|
||
function toggleDisplay (selector, value) { | ||
const input = document.querySelector(selector) | ||
input.style.display = (value) ? 'block' : 'none' | ||
} | ||
topbar.config({barColors: {0: '#63b1bc'}, shadowBlur: 0}) | ||
window.addEventListener('phx:page-loading-start', info => topbar.show()) | ||
window.addEventListener('phx:page-loading-stop', info => topbar.hide()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wdyt of adding a delay before showing the loading bar? the loading bar could be distracting if it shows and 50ms later clears; so we could only show it if the request is taking longer than (eg) 200ms. let topbarDelay = null;
window.addEventListener("phx:page-loading-start", _info => {
clearTimeout(topbarDelay);
topbarDelay = setTimeout(() => topbar.show(), 200);
})
window.addEventListener("phx:page-loading-stop", _info => {
clearTimeout(topbarDelay);
topbar.hide();
}) |
||
|
||
documentReady(function () { | ||
document | ||
.querySelectorAll('input[name="user[type]"]') | ||
.forEach((field) => { | ||
field.addEventListener('change', (e) => { | ||
toggleDisplay('div.company_name', (e.target.value === 'business')) | ||
}) | ||
}) | ||
}) | ||
liveSocket.connect() | ||
|
||
// Expose liveSocket on window for web console debug logs and latency simulation: | ||
// >> liveSocket.enableDebug() | ||
// >> liveSocket.enableLatencySim(1000) | ||
// The latency simulator is enabled for the duration of the browser session. | ||
// Call disableLatencySim() to disable: | ||
// >> liveSocket.disableLatencySim() | ||
window.liveSocket = liveSocket |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
defmodule RecognizerWeb.Accounts.TwoFactorSettingsLive do | ||
use RecognizerWeb, :live_view | ||
|
||
alias Recognizer.Accounts | ||
|
||
def mount(_params, %{"two_factor_user_id" => user_id}, socket) do | ||
user = Accounts.get_user!(user_id) | ||
changeset = Accounts.change_user_two_factor(user, %{}) | ||
|
||
{:ok, | ||
socket | ||
|> assign(:user, user) | ||
|> assign(:changeset, changeset) | ||
|> assign(:params, %{}) | ||
|> assign(:settings, %{}) | ||
|> assign(:step, :choice)} | ||
end | ||
|
||
def handle_event("choice", %{"user" => user_params}, socket) do | ||
user = socket.assigns.user | ||
|
||
changeset = Accounts.change_user_two_factor(user, user_params) | ||
{:ok, settings} = Accounts.get_new_two_factor_settings(user) | ||
|
||
{:noreply, | ||
socket | ||
|> assign(:changeset, changeset) | ||
|> assign(:params, user_params) | ||
|> assign(:settings, settings) | ||
|> assign(:step, :backup)} | ||
end | ||
|
||
def handle_event("backup", _params, socket) do | ||
notification_preferences = Ecto.Changeset.get_field(socket.assigns.changeset, :notification_preference) | ||
|
||
case notification_preferences.two_factor do | ||
:app -> | ||
{:noreply, assign(socket, :step, :app_validate)} | ||
|
||
_ -> | ||
{:noreply, assign(socket, :step, :phone_number)} | ||
end | ||
end | ||
|
||
def handle_event("app_validate", %{"user" => %{"two_factor_code" => code}}, socket) do | ||
user = socket.assigns.user | ||
|
||
with {:ok, semi_updated_user} <- Accounts.confirm_and_save_two_factor_settings(code, user), | ||
{:ok, updated_user} <- Accounts.update_user(semi_updated_user, socket.assigns.params) do | ||
{:noreply, | ||
socket | ||
|> put_flash(:info, "Two factor authentication enabled.") | ||
|> push_redirect(to: Routes.user_settings_path(socket, :edit))} | ||
else | ||
_ -> {:noreply, put_flash(socket, :error, "Two factor code is invalid.")} | ||
end | ||
end | ||
|
||
def handle_event("phone_number", %{"user" => user_attrs}, socket) do | ||
user = socket.assigns.user | ||
params = Map.merge(socket.assigns.params, user_attrs) | ||
changeset = Accounts.change_user_two_factor(user, params) | ||
|
||
if changeset.valid? do | ||
{:noreply, | ||
socket | ||
|> assign(:changeset, changeset) | ||
|> assign(:params, params) | ||
|> assign(:step, :phone_number_validate)} | ||
else | ||
{:noreply, | ||
socket | ||
|> assign(:changeset, changeset) | ||
|> assign(:params, params)} | ||
end | ||
end | ||
|
||
def handle_event("phone_number_validate", %{"user" => %{"two_factor_code" => code}}, socket) do | ||
user = socket.assigns.user | ||
|
||
with {:ok, semi_updated_user} <- Accounts.confirm_and_save_two_factor_settings(code, user), | ||
{:ok, updated_user} <- Accounts.update_user(semi_updated_user, socket.assigns.params) do | ||
{:noreply, | ||
socket | ||
|> put_flash(:info, "Two factor authentication enabled.") | ||
|> push_redirect(to: Routes.user_settings_path(socket, :edit))} | ||
else | ||
_ -> {:noreply, put_flash(socket, :error, "Two factor code is invalid.")} | ||
end | ||
end | ||
|
||
def handle_event("resend", _params, socket) do | ||
%{user: user, settings: settings} = socket.assigns | ||
|
||
Accounts.send_new_two_factor_notification( | ||
user, | ||
settings["two_factor_seed"], | ||
settings["notification_preference"]["two_factor"] | ||
) | ||
|
||
{:noreply, put_flash(socket, :info, "Two factor code sent.")} | ||
end | ||
|
||
def render(assigns) do | ||
template = to_string(assigns.step) <> ".html" | ||
RecognizerWeb.Accounts.TwoFactorSettingsView.render(template, assigns) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,10 @@ defmodule RecognizerWeb.Router do | |
pipeline :browser do | ||
plug :accepts, ["html"] | ||
plug :fetch_session | ||
plug :fetch_flash | ||
plug :fetch_live_flash | ||
plug :protect_from_forgery | ||
plug :put_secure_browser_headers | ||
plug :put_root_layout, {RecognizerWeb.LayoutView, :root} | ||
end | ||
|
||
pipeline :api do | ||
|
@@ -96,7 +97,6 @@ defmodule RecognizerWeb.Router do | |
|
||
get "/settings", UserSettingsController, :edit | ||
put "/settings", UserSettingsController, :update | ||
get "/settings/two-factor", UserSettingsController, :two_factor | ||
post "/settings/two-factor", UserSettingsController, :two_factor_confirm | ||
live "/settings/two-factor", TwoFactorSettingsLive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heads up, you could specify the it's a little awkward though because there's no real |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also need the
dom
key here so alpine components don't lose their state when liveview patches the dom. docs