Skip to content

Commit 14f9a11

Browse files
authored
Merge feat/jwt
Feat/jwt
2 parents b339b79 + 3c04141 commit 14f9a11

22 files changed

Lines changed: 228 additions & 77 deletions

File tree

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/api-ecommerce.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dataSources.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/ecto_enum.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/guardian.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/jose.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/mariaex.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/config.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ config :api_ecommerce, ApiEcommerceWeb.Endpoint,
1818
render_errors: [view: ApiEcommerceWeb.ErrorView, accepts: ~w(json)],
1919
pubsub: [name: ApiEcommerce.PubSub, adapter: Phoenix.PubSub.PG2]
2020

21+
config :api_ecommerce, ApiEcommerce.Guardian,
22+
issuer: "api_ecommerce",
23+
secret_key: "RFl88y/O+rrGqRzWwdTGpsF68o07jhz60tfUnh0mTKhkGpeGo3Adzc8+xDuxXyd5"
24+
2125
# Configures Elixir's Logger
2226
config :logger, :console,
2327
format: "$time $metadata[$level] $message\n",

lib/api_ecommerce/auth.ex

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ defmodule ApiEcommerce.Auth do
44
"""
55

66
import Ecto.Query, warn: false
7-
alias ApiEcommerce.Repo
87

8+
alias ApiEcommerce.Repo
99
alias ApiEcommerce.Auth.User
10+
alias ApiEcommerce.Guardian
1011

1112
@doc """
1213
Returns the list of users.
@@ -107,6 +108,7 @@ defmodule ApiEcommerce.Auth do
107108
query
108109
|> Repo.one()
109110
|> verify_password(password)
111+
|> gen_token()
110112
end
111113

112114
defp verify_password(nil, _) do
@@ -116,9 +118,20 @@ defmodule ApiEcommerce.Auth do
116118

117119
defp verify_password(user, password) do
118120
if Bcrypt.verify_pass(password, user.password_hash) do
119-
{:ok, user}
121+
user
120122
else
121123
{:error, "Wrong username or password"}
122124
end
123125
end
126+
127+
defp gen_token(%User{} = user) do
128+
case Guardian.encode_and_sign(user) do
129+
{:ok, token, _claims} -> {:ok, user, token}
130+
_ -> {:error, :unauthorized}
131+
end
132+
end
133+
134+
defp gen_token(error) do
135+
error
136+
end
124137
end

lib/api_ecommerce/auth/user.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ defmodule ApiEcommerce.Auth.User do
55
@primary_key {:id, :binary_id, autogenerate: true}
66
@foreign_key_type :binary_id
77
schema "users" do
8+
field :name, :string
89
field :email, :string
9-
field :is_active, :boolean, default: false
10+
field :role, RoleEnum, default: :member
11+
field :status, StatusEnum, default: :active
12+
field :recovery_token, :string
13+
field :recovery_token_created_at, :naive_datetime
1014
field :password_hash, :string
1115
field :password, :string, virtual: true
1216
field :password_confirmation, :string, virtual: true
@@ -17,8 +21,8 @@ defmodule ApiEcommerce.Auth.User do
1721
@doc false
1822
def changeset(user, attrs) do
1923
user
20-
|> cast(attrs, [:email, :is_active, :password])
21-
|> validate_required([:email, :is_active, :password])
24+
|> cast(attrs, [:name, :email, :status, :role, :password, :password_confirmation])
25+
|> validate_required([:email, :status, :role, :password, :password_confirmation])
2226
|> validate_format(:email, ~r/@/)
2327
|> validate_length(:password, min: 6)
2428
|> validate_confirmation(:password) # Check that password === password_confirmation
@@ -27,7 +31,9 @@ defmodule ApiEcommerce.Auth.User do
2731
end
2832

2933
defp put_password_hash(%Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset) do
30-
change(changeset, Bcrypt.add_hash(password))
34+
changeset
35+
|> change(Bcrypt.add_hash(password))
36+
|> change(%{password_confirmation: nil})
3137
end
3238

3339
defp put_password_hash(changeset) do

0 commit comments

Comments
 (0)