Elixir Phoenix: Creating An App With Tests (Part 5: Setting a Current User)
Updated: January 16th, 2019
Current user (GitHub)
Now that we can sign in, how do we tell we are signed in? It would be great if there was some sort of greeting on the navbar or maybe the navbar’s “Sign in with Google” changed to “Sign out” because we were already signed in. We will add that functionality next.
First things first, we will want to write a test to check our navbar for the proper links whether a user is signed in or not. Before we can do that, we need to set up a test_helpers
file to allow us to quickly create users and videos for our tests. We will do this by adding to our test/support
directory a file called test_helpers.ex
(this is not to be mistaken for the test_helper
file that is already in the test
directory).
Inside our new test_helpers
file we will create a fixture function that will create a user for us (1f18dc6).
Next, we will import our TestHelpers
to the DataCase
module and the ConnCase
module (0d9a519). This will ensure our fixture is available for our tests.
We will add a test to our navigation_test.exs
. The added test will ensure we show our “Sign out” link when we are signed in (59af37f).
If we run mix test
now, our new test will be failing. We will now create our set_user
plug to get the failed test passing.
In our lib/catcasts_web
directory, we will create a new directory called plugs
. Inside the plugs
directory, we will create a file called set_user.ex
.
With our create
action in our session_controller
, we have |> put_session(:user_id, user.id)
. This puts the user.id
of our user into the session when we sign them in. We access the session in call/2
and grab the user.id
. Unless we already have an assigned user, we look in our database to find the user, and assign our user to conn
. To make use of this in our application, we need to tell our pipeline :browser
in our router.ex
to use our plug (d0a7daa).
Now that we have access to our user, we can conditionally show navbar items or text. In our _navigation.html.eex
, we are going to replace our “Sign in with Google” link with a conditional that will show different links depending on if we are signed in or not. We are also going to add a little message that welcomes the user by their first name.
Here is the entirety of our new _navigation.html.eex
(0ac23f5).
If your server is running and you are signed in, you will see that this will break our page because the sign out link does not exist. We will first update our session_controller_test
and add a test for signing out a user (a813f99).
Running mix test
gives us an error stating ** (UndefinedFunctionError) function CatcastsWeb.SessionController.request/2 is undefined or private
. This is telling us that the route does not exist. We will add get "/signout", SessionController, :delete
inside our router.ex
in the scope "/auth"
block (ac4a0a3).
Running mix test
again tells us that our SessionController
does not have a delete/2
function. Let’s open up our SessionController
and take care of adding it after our create/2
function (7ced98b).
If we run mix test
one more time we will see that all of our tests are passing!
Part 1: The Setup
Part 2: Using Tailwind CSS
Part 3: Navbar, Errors, and Homepage
Part 4: Using Google Ueberauth
Part 5: Setting a Current User
Next Up:
Part 6: Using the YouTube API
Part 7: Setting Up Authorizations
Part 8: Making Things Look Nicer
Part 9: Search, Sort, Pagination
Part 10 Bonus: Using Elixir’s 1.6 Code Formatter
Follow me on Twitter @thebrianemory. Follow me here, click the hands below to show some appreciation, leave a comment, and get in touch!