[Project A] Devlog #1: Testing User Apis
A little background
Before I get into what I did, I want to give a brief introduction to what this project is, what it's about, and its purpose. I will do this for the other projects I have in my other devlogs so that the folks at home are up to speed. This is sort of a personal project but I can't make it open to the public so I've decided to call it `Project A`. I don't think I'll stick with this naming convention for the rest of the projects but I couldn't think of a better name at the time of writing.
So this project is for managing attendance for a group of people within a certain organization. Now, let's get into the log.
Brief History
I recently started learning Elixir and Phoenix (the popular web framework for elixir) because I wanted to use them as the backend for this project. Initially, the backend was built using supabase but I had to switch to developing my own because there were some additions that came in later that would be cumbersome to implement in supabase so I just decided to learn a new language/framework while I build the backend. I decided to go with Elixir because I heard it's great for building realtime applications and I wanted some part of the application to be realtime. I try to learn something new with every project that I work on so I might as well go with a new language/framework.
Actual Devlog (This time for real, maybe)
We can finally get started.
Side note: I am starting this devlog after some work has already been done so I'm just going start from what I did today and recap what I have already done previously.
I used Phoenix Contexts to scaffold quite a bit of the models/controllers that I needed but I decided to stop using it because it was making it too easy (I get that that's the point, but as a newcomer to the language and framework, I would prefer to do those myself so that I understand what the context does so that later on it becomes easier for me. I was sort of taken aback by how easy the context made the development process). This app is primarily going to be a crud app so having the context generate almost all that I need was kinda taking the fun out of the project. I did however continue to use the structure that the phoenix contexts provide because I think it is a good structure to follow.
I would implore you to read about phoenix contexts so that you understand where I am coming from.
I stopped using the context right about the time I was about to start working on the User side of the project (login/signup/invite, etc)
For now, the methods I have defined for my UserController are just the index & show(I will add more later, I just needed to test if my implementation was working)
To test my apis, I used elixir's interactive shell (iex) to start the server. The command is iex -S mix phx.server
.
I used Guardian to secure the rest of the apis that need authentication. I had a little bit of help from ChatGPT to set it up, however, I wanted to use the `/api/v1/` prefix for the auth routes as well as my routes that needed to be secured. So that required that I updated my :api pipeline.
Initial
pipeline :api do
plug :accepts, ["json"]
plug Guardian.Plug.VerifyHeader
plug Guardian.Plug.LoadResource
end
Current
pipeline :api do
plug :accepts, ["json"]
end
I then added a new pipeline for guardian so that this could be used in the scope for the api.
pipeline :guardian do
plug Guardian.Plug.Pipeline,
module: MyGuardianModule,
error_handler: MyGuardianErrorHandler
plug Guardian.Plug.VerifyHeader, scheme: "Bearer"
plug Guardian.Plug.LoadResource
end
Initial
scope "/api/v1", MyAppWeb do
pipe_through :api
post "/login", SessionController, :login
... rest of scope
end
Current
scope "/api/v1", MyAppWeb do
pipe_through :api
pipe_through :guardian
...rest of scope
end
scope "/api/v1", MyAppWeb do
pipe_through :api
post "/login", SessionController, :login
end
This way, the login route could still use the :api pipeline but then since it doesn't need to be authenticated it would just be processed normally.
I create a user in iex and then tested using insomnia to see if my APIs were sending the write response. While I was learning phoenix, I was using the templating engine so I misunderstood how the views worked when it came to rendering json for APIs. I was under the impression that maybe elixir would figure out what I wanted to send based on the data I passed and then send it but I have now come to understand that I need to define my own render
methods so that they can display what I need to display.
I think this is a good point to end for now (since this is where I stopped today). I will continue hopefully tomorrow.
If this feels a little bit rumbly that's because I didn't gather my thoughts properly before writing this, so these are my raw thoughts. I think I will continue with this style for now, but maybe I'll change it up. Time will tell. It's been a while since I wrote something like this and I hope to improve by being consistent.
Until next time, I bid you adieu.