Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93631f195f | ||
|
|
cde8970b46 |
@@ -49,3 +49,7 @@
|
|||||||
Update Links.create_one to use a function.
|
Update Links.create_one to use a function.
|
||||||
** 0.5.2 <2023-08-05 Sat>
|
** 0.5.2 <2023-08-05 Sat>
|
||||||
Add dependencies for API Authentication.
|
Add dependencies for API Authentication.
|
||||||
|
** 0.6.0 <2023-08-05 Sat>
|
||||||
|
Add User model.
|
||||||
|
** 0.7.0 <2023-08-05 Sat>
|
||||||
|
Add the API Authentication.
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ config :logger, :console,
|
|||||||
# Use Jason for JSON parsing in Phoenix
|
# Use Jason for JSON parsing in Phoenix
|
||||||
config :phoenix, :json_library, Jason
|
config :phoenix, :json_library, Jason
|
||||||
|
|
||||||
|
config :link_shortener, LinkShortenerWeb.Auth.Guardian,
|
||||||
|
issuer: "link_shortener",
|
||||||
|
secret_key: System.get_env("SECRET_KEY_BASE")
|
||||||
|
|
||||||
# Import environment specific config. This must remain at the bottom
|
# Import environment specific config. This must remain at the bottom
|
||||||
# of this file so it overrides the configuration defined above.
|
# of this file so it overrides the configuration defined above.
|
||||||
import_config "#{config_env()}.exs"
|
import_config "#{config_env()}.exs"
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
defmodule LinkShortener.Accounts do
|
||||||
|
@moduledoc """
|
||||||
|
The Accounts context.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import Ecto.Query, warn: false
|
||||||
|
alias LinkShortener.Repo
|
||||||
|
|
||||||
|
alias LinkShortener.Accounts.User
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns the list of users.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> list_users()
|
||||||
|
[%User{}, ...]
|
||||||
|
|
||||||
|
"""
|
||||||
|
def list_users do
|
||||||
|
Repo.all(User)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Gets a single user.
|
||||||
|
|
||||||
|
Raises `Ecto.NoResultsError` if the User does not exist.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> get_user!(123)
|
||||||
|
%User{}
|
||||||
|
|
||||||
|
iex> get_user!(456)
|
||||||
|
** (Ecto.NoResultsError)
|
||||||
|
|
||||||
|
"""
|
||||||
|
def get_user!(id), do: Repo.get!(User, id)
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Creates a user.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> create_user(%{field: value})
|
||||||
|
{:ok, %User{}}
|
||||||
|
|
||||||
|
iex> create_user(%{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def create_user(attrs \\ %{}) do
|
||||||
|
%User{}
|
||||||
|
|> User.changeset(attrs)
|
||||||
|
|> Repo.insert()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Updates a user.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> update_user(user, %{field: new_value})
|
||||||
|
{:ok, %User{}}
|
||||||
|
|
||||||
|
iex> update_user(user, %{field: bad_value})
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def update_user(%User{} = user, attrs) do
|
||||||
|
user
|
||||||
|
|> User.changeset(attrs)
|
||||||
|
|> Repo.update()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Deletes a user.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> delete_user(user)
|
||||||
|
{:ok, %User{}}
|
||||||
|
|
||||||
|
iex> delete_user(user)
|
||||||
|
{:error, %Ecto.Changeset{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def delete_user(%User{} = user) do
|
||||||
|
Repo.delete(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Returns an `%Ecto.Changeset{}` for tracking user changes.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
iex> change_user(user)
|
||||||
|
%Ecto.Changeset{data: %User{}}
|
||||||
|
|
||||||
|
"""
|
||||||
|
def change_user(%User{} = user, attrs \\ %{}) do
|
||||||
|
User.changeset(user, attrs)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
defmodule LinkShortener.Accounts.User do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
schema "users" do
|
||||||
|
field :email, :string
|
||||||
|
field :encrypted_password, :string
|
||||||
|
field :password, :string, virtual: true
|
||||||
|
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
def changeset(user, attrs) do
|
||||||
|
user
|
||||||
|
|> cast(attrs, [:email, :password])
|
||||||
|
|> validate_required([:email, :password])
|
||||||
|
|> unique_constraint(:email)
|
||||||
|
|> put_hashed_password()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp put_hashed_password(changeset) do
|
||||||
|
case changeset do
|
||||||
|
%Ecto.Changeset{valid?: true, changes: %{password: password}}
|
||||||
|
->
|
||||||
|
put_change(changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(password))
|
||||||
|
_ ->
|
||||||
|
changeset
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
defmodule LinkShortenerWeb.Auth.Guardian do
|
||||||
|
use Guardian, otp_app: :link_shortener
|
||||||
|
|
||||||
|
alias LinkShortener.Accounts
|
||||||
|
|
||||||
|
def subject_for_token(user, _claims) do
|
||||||
|
sub = to_string(user.id)
|
||||||
|
{:ok, sub}
|
||||||
|
end
|
||||||
|
|
||||||
|
def resource_from_claims(claims) do
|
||||||
|
id = claims["sub"]
|
||||||
|
resource = Accounts.get_user!(id)
|
||||||
|
{:ok, resource}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
defmodule LinkShortenerWeb.Api.V1.UserController do
|
||||||
|
use LinkShortenerWeb, :controller
|
||||||
|
|
||||||
|
alias LinkShortener.Accounts
|
||||||
|
alias LinkShortener.Accounts.User
|
||||||
|
alias LinkShortenerWeb.Auth.Guardian
|
||||||
|
|
||||||
|
action_fallback LinkShortenerWeb.FallbackController
|
||||||
|
|
||||||
|
def create(conn, %{"user" => user_params}) do
|
||||||
|
with {:ok, %User{} = user} <- Accounts.create_user(user_params),
|
||||||
|
{:ok, token, _claims} <- Guardian.encode_and_sign(user) do
|
||||||
|
conn
|
||||||
|
|> put_status(:created)
|
||||||
|
|> render("user.json", %{user: user, token: token})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def signin(conn, %{"email" => email, "password" => password}) do
|
||||||
|
with {:ok, user, token} <- Guardian.authenticate(email, password) do
|
||||||
|
conn
|
||||||
|
|> put_status(:created)
|
||||||
|
|> render("user.json", %{user: user, token: token})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -27,6 +27,8 @@ defmodule LinkShortenerWeb.Router do
|
|||||||
|
|
||||||
scope "/v1", Api.V1, as: :v1 do
|
scope "/v1", Api.V1, as: :v1 do
|
||||||
resources "/links", LinkController
|
resources "/links", LinkController
|
||||||
|
post "/users/signup", UserController, :create
|
||||||
|
post "/users/signin", UserController, :signin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
defmodule LinkShortenerWeb.Api.V1.UserView do
|
||||||
|
use LinkShortenerWeb, :view
|
||||||
|
|
||||||
|
alias LinkShortenerWeb.Api.V1.UserView
|
||||||
|
|
||||||
|
def render("user.json", %{user: user, token: token}) do
|
||||||
|
%{
|
||||||
|
email: user.email,
|
||||||
|
token: token
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
defmodule LinkShortener.Repo.Migrations.CreateUsers do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
create table(:users) do
|
||||||
|
add :email, :string
|
||||||
|
add :encrypted_password, :string
|
||||||
|
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
create unique_index(:users, [:email])
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user