diff --git a/lib/book_my_gigs/accounts.ex b/lib/book_my_gigs/accounts.ex index 31b3949..2bc96b6 100644 --- a/lib/book_my_gigs/accounts.ex +++ b/lib/book_my_gigs/accounts.ex @@ -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( @@ -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 diff --git a/lib/book_my_gigs/accounts/Storage/account.ex b/lib/book_my_gigs/accounts/Storage/account.ex index ed0d621..6b1675c 100644 --- a/lib/book_my_gigs/accounts/Storage/account.ex +++ b/lib/book_my_gigs/accounts/Storage/account.ex @@ -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 diff --git a/lib/book_my_gigs_web/accounts/accounts_controller.ex b/lib/book_my_gigs_web/accounts/accounts_controller.ex index 24ed617..034dca8 100644 --- a/lib/book_my_gigs_web/accounts/accounts_controller.ex +++ b/lib/book_my_gigs_web/accounts/accounts_controller.ex @@ -41,6 +41,7 @@ defmodule BookMyGigsWeb.AccountsController do account = params |> Accounts.create_account() + |> IO.inspect(label: "this is here") |> Jason.encode!() conn diff --git a/lib/book_my_gigs_web/accounts/schemas/Account_response.ex b/lib/book_my_gigs_web/accounts/schemas/Account_response.ex index 9c199d7..1505a5e 100644 --- a/lib/book_my_gigs_web/accounts/schemas/Account_response.ex +++ b/lib/book_my_gigs_web/accounts/schemas/Account_response.ex @@ -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 diff --git a/lib/book_my_gigs_web/accounts/schemas/get_accounts_response.ex b/lib/book_my_gigs_web/accounts/schemas/get_accounts_response.ex index 078227a..afa80d5 100644 --- a/lib/book_my_gigs_web/accounts/schemas/get_accounts_response.ex +++ b/lib/book_my_gigs_web/accounts/schemas/get_accounts_response.ex @@ -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" } ] })