Skip to content

Commit

Permalink
Use verified routes
Browse files Browse the repository at this point in the history
  • Loading branch information
cruessler committed Oct 29, 2023
1 parent 76d9fce commit c48318e
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 19 deletions.
21 changes: 15 additions & 6 deletions lib/ex_rss_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ defmodule ExRssWeb do
below.
"""

# `exrss` has not been set up to use verified routes yet. To change that,
# follow [the guide][1].
#
# [1]: https://gist.github.com/chrismccord/00a6ea2a96bc57df0cce526bd20af8a7
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)

def model do
quote do
Expand All @@ -39,8 +36,10 @@ defmodule ExRssWeb do
import Ecto
import Ecto.Query

import Plug.Conn
import ExRssWeb.Gettext
alias ExRssWeb.Router.Helpers, as: Routes

unquote(verified_routes())
end
end

Expand All @@ -56,9 +55,10 @@ defmodule ExRssWeb do

import ExRssWeb.ErrorHelpers
import ExRssWeb.Gettext
alias ExRssWeb.Router.Helpers, as: Routes

import ExRssWeb.AppView

unquote(verified_routes())
end
end

Expand All @@ -79,6 +79,15 @@ defmodule ExRssWeb do
end
end

def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: ExRssWeb.Endpoint,
router: ExRssWeb.Router,
statics: ExRssWeb.static_paths()
end
end

@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_rss_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule ExRssWeb.SessionController do
|> put_session(:current_user, user)
|> Session.renew_remember_me_cookie()
|> put_flash(:info, "You are now logged in.")
|> redirect(to: Routes.feed_path(conn, :index))
|> redirect(to: ~p"/feeds")
else
:error ->
conn
Expand All @@ -28,6 +28,6 @@ defmodule ExRssWeb.SessionController do
conn
|> delete_session(:current_user)
|> put_flash(:info, "You are now logged out.")
|> redirect(to: Routes.page_path(conn, :index))
|> redirect(to: ~p"/")
end
end
2 changes: 1 addition & 1 deletion lib/ex_rss_web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule ExRssWeb.UserController do
{:ok, _} ->
conn
|> put_flash(:info, "Your account has been registered.")
|> redirect(to: Routes.page_path(conn, :index))
|> redirect(to: ~p"/")

{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_rss_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule ExRssWeb.Endpoint do
at: "/",
from: :ex_rss,
gzip: false,
only: ~w(assets fonts images favicon.ico robots.txt)
only: ExRssWeb.static_paths()

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_rss_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<meta name="author" content="">

<title>Hello ExRss!</title>
<link phx-track-static rel="stylesheet" href="<%= Routes.static_path(@conn, "/assets/app.css") %>">
<script phx-track-static src="<%= Routes.static_path(@conn, "/assets/app.js") %>"></script>
<link phx-track-static rel="stylesheet" href="<%= ~p"/assets/app.css" %>">
<script phx-track-static src="<%= ~p"/assets/app.js" %>"></script>
</head>

<body>
Expand Down
8 changes: 4 additions & 4 deletions lib/ex_rss_web/templates/page/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
Hello <%= @current_user.email %>!
</p>
<p>
Your feeds can be found at <a href="<%= Routes.feed_path(@conn, :index) %>">
<%= Routes.feed_path(@conn, :index) %></a>.
Your feeds can be found at <a href="<%= ~p"/feeds" %>">
<%= ~p"/feeds" %></a>.
</p>
<% else %>
<p>
Right now, there is not much you can do. Maybe you want to
<a href="<%= Routes.user_path(@conn, :new) %>">register</a>?
<a href="<%= ~p"/users/new" %>">register</a>?
If you are already registered you can of course
<a href="<%= Routes.session_path(@conn, :new) %>">log in</a>.
<a href="<%= ~p"/session/new" %>">log in</a>.
</p>
<% end %>
2 changes: 1 addition & 1 deletion lib/ex_rss_web/templates/session/new.html.eex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= form_for @conn,
Routes.session_path(@conn, :create),
~p"/session",
[as: "session", class: :"session-new"],
fn f -> %>
<h2>Log in</h2>
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_rss_web/templates/user/new.html.eex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="container">
<%= form_for @changeset,
Routes.user_path(@conn, :create),
~p"/users",
[class: :"user-new"],
fn f -> %>
<h1>Register</h1>
Expand Down
3 changes: 2 additions & 1 deletion test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule ExRssWeb.ConnCase do

using do
quote do
use ExRssWeb, :verified_routes

# Import conveniences for testing with connections
import Plug.Conn
import Phoenix.ConnTest
Expand All @@ -26,7 +28,6 @@ defmodule ExRssWeb.ConnCase do
import Ecto.Changeset
import Ecto.Query

import ExRssWeb.Router.Helpers
import ExRssWeb.Api.Helpers

# The default endpoint for testing
Expand Down

0 comments on commit c48318e

Please sign in to comment.