This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/phoenix_admin/accounts/current_user.ex | |
defmodule PhoenixAdmin.Accounts.CurrentUser do | |
import Plug.Conn | |
import Guardian.Plug | |
def init(opts), do: opts | |
def call(conn, _opts) do | |
current_user = current_resource(conn) | |
assign(conn, :current_user, current_user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/phoenix_admin_web/views/session_view.ex | |
defmodule PhoenixAdminWeb.SessionView do | |
use PhoenixAdminWeb, :view | |
end | |
# lib/phoenix_admin_web/template/new.html.eex | |
<%= form_for @changeset, Routes.session_path(@conn, :login), fn f -> %> | |
<%= if @changeset.action do %> | |
<div class="alert alert-danger"> | |
<p>Oops, something went wrong! Please check the errors below.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PhoenixAdminWeb.SessionController do | |
use PhoenixAdminWeb, :controller | |
alias PhoenixAdmin.Accounts | |
alias PhoenixAdmin.Accounts.User | |
def new(conn, _) do | |
changeset = Accounts.change_user(%User{}) | |
conn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/phoenix_admin/accounts/user.ex | |
defmodule PhoenixAdmin.Accounts.User do | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias Argon2 | |
schema "users" do | |
field :email, :string | |
field :name, :string | |
field :password, :string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/phoenix_admin/accounts/pipeline.ex | |
defmodule PhoenixAdmin.Accounts.Pipeline do | |
use Guardian.Plug.Pipeline, | |
otp_app: :phoenix_admin, | |
module: PhoenixAdmin.Accounts.Guardian, | |
error_handler: PhoenixAdmin.Accounts.ErrorHandler | |
# セッショントークンの場合の検証 | |
plug Guardian.Plug.VerifySession | |
# 認証ヘッダーの場合の検証 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/phoenix_admin_web/controllers/user_controller.ex | |
alias PhoenixAdmin.Accounts | |
alias PhoenixAdmin.Accounts.User | |
# alias PhoenixAdmin.{Accounts, Accounts.User, ..} とまとめて書ける | |
def create(conn, %{"user" => user_params}) do | |
case Accounts.create_user(user_params) do | |
{:ok, user} -> | |
conn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def authenticate_user(email, plain_text_password) do | |
query = from u in User, where: u.email == ^email | |
case Repo.one(query) do | |
nil -> | |
Argon2.no_user_verify() | |
{:error, :invalid_credentials} | |
user -> | |
if Argon2.verify_pass(plain_text_password, user.password) do | |
{:ok, user} | |
else |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/phoenix_admin/accounts/error_handler.ex | |
defmodule PhoenixAdmin.Accounts.ErrorHandler do | |
import Plug.Conn | |
@behaviour Guardian.Plug.ErrorHandler | |
@impl Guardian.Plug.ErrorHandler | |
def auth_error(conn, {type, _reason}, _opts) do | |
body = to_string(type) | |
conn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/phoenix_admin/accounts/guardian.ex | |
defmodule PhoenixAdmin.Accounts.Guardian do | |
use Guardian, otp_app: :phoenix_admin | |
alias PhoenixAdmin.Accounts | |
def subject_for_token(resource, _claims) do | |
sub = to_string(resource.id) | |
{:ok, sub} | |
end |