Skip to content

Instantly share code, notes, and snippets.

@TeijiW
Created June 8, 2021 12:44
Show Gist options
  • Save TeijiW/d07473df76e5111b35841463da660cf5 to your computer and use it in GitHub Desktop.
Save TeijiW/d07473df76e5111b35841463da660cf5 to your computer and use it in GitHub Desktop.
defmodule ProjectWeb.UserSessionLive do
use ProjectWeb, :live_view
alias Project.Accounts
def mount(_params, _session, socket) do
{:ok,
assign(socket,
email_input_error: nil,
email: nil
)}
end
def handle_event("login", %{"user" => %{"email" => email}}, socket) do
Accounts.get_user_by_email(email)
|> handle_login_response(socket)
end
defp handle_login_response(%Accounts.User{} = user, socket)do
Accounts.send_access_code(user.email)
{:noreply, assign(socket, %{email: user.email, email_input_error: nil})}
end
defp handle_login_response(_user, socket),
do: {:noreply, assign(socket, :email_input_error, "E-mail not found")}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment