Skip to content

Commit

Permalink
feat(accounts): added hash_password/1 to create_account endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabrielparizet committed Jun 6, 2024
1 parent 51ab913 commit 4fe630c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
23 changes: 22 additions & 1 deletion lib/book_my_gigs/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ defmodule BookMyGigs.Accounts do
end

def create_account(%{"account" => account_params}) do
Storage.create_account(account_params)
hash_password = hash_password(account_params["password"])

account_params
|> Map.put("password", hash_password)
|> Storage.create_account()
|> delete_password_from_response()
end

def update_account(
Expand All @@ -49,4 +54,20 @@ defmodule BookMyGigs.Accounts do
def delete_account(id) do
Storage.delete_account(id)
end

defp hash_password(password) do
hash = Bcrypt.hash_pwd_salt(password)

if Bcrypt.verify_pass(password, hash) == true do
hash
else
raise("Error: password and its hash do not match")
end
end

defp delete_password_from_response(account_response) do
account_map = Map.from_struct(account_response)
account_map_without_password = Map.delete(account_map, :password)
struct!(account_response.__struct__, account_map_without_password)
end
end
8 changes: 0 additions & 8 deletions lib/book_my_gigs/accounts/Storage/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ defmodule BookMyGigs.Accounts.Storage.Account do
|> validate_required([:email, :password])
|> unique_constraint(:email)
|> validate_format(:email, ~r/@/)
|> validate_format(
:password,
~r/^(?=.*[!?;:@*=+])(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).+$/,
[
{:message,
"Password must have a minimum of 8 characters long, include 1 downcase letter, 1 capital letter, 1 number and a special character (!?;:@*=+)"}
]
)
|> validate_length(:password, min: 8)
end
end
1 change: 1 addition & 0 deletions lib/book_my_gigs_web/accounts/accounts_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule BookMyGigsWeb.AccountsController do
account =
params
|> Accounts.create_account()
|> IO.inspect(label: "this is here")

Check warning on line 44 in lib/book_my_gigs_web/accounts/accounts_controller.ex

View workflow job for this annotation

GitHub Actions / build

There should be no calls to `IO.inspect/1`.
|> Jason.encode!()

conn
Expand Down
6 changes: 1 addition & 5 deletions lib/book_my_gigs_web/accounts/schemas/Account_response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ defmodule BookMyGigsWeb.Accounts.Schemas.AccountResponse do
properties: %{
email: %Schema{
type: :string
},
password: %Schema{
type: :string
}
}
}
},
example: %{
"email" => "test@gmail.com",
"password" => "ThisIsMyPassword123?"
"email" => "test@gmail.com"
}
})
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ defmodule BookMyGigsWeb.Accounts.Schemas.GetAccountsResponse do
properties: %{
email: %Schema{
type: :string
},
password: %Schema{
type: :string
}
}
},
example: [
%{
"email" => "test@email.com",
"password" => "ThisIsMyPassword123?"
"email" => "test@email.com"
},
%{
"email" => "test@email.com",
"password" => "ThisIsMyPassword123?"
"email" => "test@email.com"
}
]
})
Expand Down

0 comments on commit 4fe630c

Please sign in to comment.