Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Redirect on login & login after creating account
Browse files Browse the repository at this point in the history
If an unauthenticated user selects a job and logs in when prompted
(including via account creation), they will be redirected to the job
they previously attempted to view.
  • Loading branch information
cwc committed Oct 21, 2016
1 parent da0d03b commit abdff66
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
7 changes: 5 additions & 2 deletions web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@ defmodule ElixirJobs.PageController do
|> redirect(to: "/")
end

defp authenticate(conn, _params) do
defp authenticate(conn, params) do
if is_nil(get_session(conn, :user)) do
conn |> put_flash(:error, "You need to login first") |> redirect(to: "/users/login") |> halt
conn
|> put_flash(:error, "You need to login first")
|> put_flash(:redir, conn.request_path)
|> redirect(to: "/users/login") |> halt
else
conn
end
Expand Down
23 changes: 17 additions & 6 deletions web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule ElixirJobs.UserController do

def login(conn, _params) do

render conn, "login.html"
render conn, "login.html", flash: get_flash(conn)
end

def create(conn, params) do
Expand All @@ -24,21 +24,32 @@ defmodule ElixirJobs.UserController do

q = Query.table("users")
|> Query.insert(user)
Repo.run(q)

conn
|> put_flash(:info, "Super! Your account created.")
|> redirect(to: "/")
result = Repo.run(q)

if result.data["errors"] == 0 do
conn
|> put_session(:user, params["email"])
|> put_flash(:info, "Super! Your account created.")
|> redirect(to: params["redir"] || "/")
|> halt
else
conn
|> put_flash(:error, "Account creation failed, please try again")
|> redirect(to: "/users/login")
end
end

def process_login(conn, params) do
if is_nil(do_login(params["email"], params["password"])) do
conn
|> put_flash(:redir, params["redir"])
|> put_flash(:error, "Login failed") |> redirect(to: "/users/login") |> halt
else
conn = put_session(conn, :user, params["email"])
conn
|> put_flash(:info, "Thanks for logging in!") |> redirect(to: "/")
|> put_flash(:info, "Thanks for logging in!")
|> redirect(to: params["redir"] || "/")
end
end

Expand Down
4 changes: 2 additions & 2 deletions web/templates/user/login.html.eex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="ui segment">
<div class="ui relaxed grid">
<div class="eight wide column">
<form class="ui form" action="<%= user_path(@conn, :process_login) %>" method="post" data-parsley-validate>
<form class="ui form" action="<%= user_path(@conn, :process_login, @flash) %>" method="post" data-parsley-validate>
<div class="ui dividing blue header">Login</div>
<input type="hidden" name="_csrf_token" value="<%= get_csrf_token() %>">
<div class="field">
Expand All @@ -21,7 +21,7 @@
OR
</div>
<div class="eight wide column">
<form class="ui form" action="<%= user_path(@conn, :create) %>" method="post" data-parsley-validate>
<form class="ui form" action="<%= user_path(@conn, :create, @flash) %>" method="post" data-parsley-validate>
<div class="ui dividing orange header">New User</div>
<input type="hidden" name="_csrf_token" value="<%= get_csrf_token() %>">
<div class="field">
Expand Down

0 comments on commit abdff66

Please sign in to comment.