enforce token authorization

master
Stefan Haslinger (root) 2022-03-25 08:46:31 +01:00
parent 64d49fea81
commit 088351899e
1 changed files with 23 additions and 11 deletions

View File

@ -11,20 +11,32 @@ defmodule WeatherTrackerWeb.WeatherConditionsController do
def create(conn, params) do
IO.inspect(params)
case WeatherConditions.create_entry(params) do
{:ok, weather_condition = %WeatherCondition{}} ->
Logger.debug("Successfully created a weather condition entry")
token =
get_req_header(conn, "authorization")
|> List.first()
conn
|> put_status(:created)
|> json(weather_condition)
if token == "shiqbNfVhL91JZOtqK0896BYJfZbUDrI2ERIzmoc" do
case WeatherConditions.create_entry(params) do
{:ok, weather_condition = %WeatherCondition{}} ->
Logger.debug("Successfully created a weather condition entry")
error ->
Logger.warn("Failed to create a weather entry: #{inspect(error)}")
conn
|> put_status(:created)
|> json(weather_condition)
conn
|> put_status(:unprocessable_entity)
|> json(%{message: "Poorly formatted payload"})
error ->
Logger.warn("Failed to create a weather entry: #{inspect(error)}")
conn
|> put_status(:unprocessable_entity)
|> json(%{message: "Poorly formatted payload"})
end
else
Logger.warn("No valid auth token provided")
conn
|> put_status(:unprocessable_entity)
|> json(%{message: "Auth token not valid"})
end
end
end