Skip to content

Commit

Permalink
[WiP] create verify_email/2 for #63
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed May 4, 2020
1 parent 110f833 commit 97c8b4c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 25 deletions.
19 changes: 17 additions & 2 deletions lib/auth/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ defmodule Auth.Person do
|> cast(attrs, [:email])
end

def password_prompt_changeset(attrs) do
%Person{}
|> cast(attrs, [:email, :password])
end

@doc """
`transform_github_profile_data_to_person/1` transforms the profile data
received from invoking `ElixirAuthGithub.github_auth/1`
Expand Down Expand Up @@ -167,9 +172,19 @@ defmodule Auth.Person do
# |> IO.inspect(label: "changeset with :email_hash")
end

def get_status_verified do
status = Auth.Status.upsert_status("verified")
status.id
end

def put_email_status_verified(changeset) do
status_verified = Auth.Status.upsert_status("verified")
put_change(changeset, :status, status_verified.id)
# IO.inspect(changeset, label: "changeset")
provider = changeset.changes.auth_provider
if provider == "google" or provider == "github" do
put_change(changeset, :status, get_status_verified())
else
changeset
end
end

# defp put_pass_hash(changeset) do
Expand Down
74 changes: 53 additions & 21 deletions lib/auth_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,37 +134,69 @@ defmodule AuthWeb.AuthController do
IO.inspect(email, label: "email")
# email is blank or invalid:
if is_nil(email) or not Fields.Validate.email(email) do
conn # re-render the login/register form:
conn # email invalid, re-render the login/register form:
|> index(params)
else
IO.puts("email is NOT nil: " <> email)
person = Auth.Person.get_person_by_email(email)
IO.inspect(person, label: "person:142")
# check if the email exists in the people table:
person = case Auth.Person.get_person_by_email(email) do
person ->
person
nil ->
person = Auth.Person.create_person(%{email: email})
IO.inspect(person, label: "person:146")
Auth.Email.sendemail(%{
email: email,
link: make_verify_link(conn, person, state)
}) |> IO.inspect(label: "sendemail")

person
person = if is_nil(person) do
person = Auth.Person.create_person(%{
email: email,
auth_provider: "email"
})
# IO.inspect(person, label: "person:146")
Auth.Email.sendemail(%{ email: email, template: "verify",
link: make_verify_link(conn, person, state),
subject: "Please Verify Your Email Address"
})

person
else
person
end
IO.inspect(person, label: "person:156")
if not is_nil(person.status) and person.status == 1 do # verified
conn
|> assign(:action, Routes.auth_path(conn, :login_register_handler))
|> render("password-prompt.html",
changeset: Auth.Person.password_prompt_changeset(%{email: email}),
state: state,
person_id: AuthWeb.ApikeyController.encrypt_encode(person.id) # hide
)
else
# respond
conn
|> put_resp_content_type("text/html")
|> send_resp(200, "login_register_handler " <> email)
|> halt()
end
IO.inspect(person)
# respond
conn
|> put_resp_content_type("text/html")
|> send_resp(200, "login_register_handler")
|> halt()
end
end

def make_verify_link(conn, person, state) do
AuthPlug.Helpers.get_baseurl_from_conn(conn)
<> "/person/verify"
<> "/auth/verify?id="
<> AuthWeb.ApikeyController.encrypt_encode(person.id)
<> "?" <> state
<> "&referer=" <> state
end

def verify_email(conn, params) do
IO.inspect(params, label: "params:196")
referer = params["referer"]
IO.inspect(referer, label: "referer:198")
person_id = AuthWeb.ApikeyController.decode_decrypt(params["id"])
IO.inspect(person_id, label: "person_id:190")

auth_client_id = get_client_id_from_query(conn)
IO.inspect(auth_client_id, label: "auth_client_id:200")
# ref = get_referer(conn)
# IO.inspect(ref, label: "referer:188")
conn
|> put_resp_content_type("text/html")
|> send_resp(200, "verify_email")
|> halt()
end


Expand Down
3 changes: 2 additions & 1 deletion lib/auth_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule AuthWeb.Router do
get "/", AuthController, :index
get "/auth/github/callback", AuthController, :github_handler
get "/auth/google/callback", AuthController, :google_handler
post "/people/register", AuthController, :login_register_handler
get "/auth/verify", AuthController, :verify_email
post "/auth/register", AuthController, :login_register_handler
end


Expand Down
2 changes: 1 addition & 1 deletion lib/auth_web/templates/auth/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@
</div>
</div>
<!-- Wake the Email Heroku App github.com/dwyl/auth/issues/50 -->
<img src="https://dwylmail.herokuapp.com/pixel" />
<img src="https://dwylmail.herokuapp.com/pixel" width="1" />
21 changes: 21 additions & 0 deletions lib/auth_web/templates/auth/password-prompt.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="phx-hero w-100 center">
<div class="w-60">
<h1 class="center tc">Please Type Your Password:</h1>

<%= form_for @changeset, @action, fn f -> %>
<%= password_input f, :password,
class: "db w-100 mt2 pa3 ba b--dark-grey f3"%>
<%= error_tag f, :password %>


<br />
<%= hidden_input f, :state, id: "state", value: @state %>

<%= submit "Login",
class: "w-100 pointer ba border-box dwyl-bg-mint white pa3 ml1 mv1 f3
shadow-hover bg-animate hover-dwyl-teal-darkest no-underline db",
style: "margin:0 auto; border-color: #318d7b;"
%>
<% end %>
</div>
</div>

0 comments on commit 97c8b4c

Please sign in to comment.