Skip to content

Instantly share code, notes, and snippets.

@8zca
8zca / phoenix_admin_accounts_current_user.ex
Created January 19, 2020 16:37
lib/phoenix_admin/accounts/current_user.ex
# 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)
@8zca
8zca / phoenix_admin_web_session_view_template.ex
Last active January 19, 2020 14:58
lib/phoenix_admin_web/views/session_view.ex
# 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>
@8zca
8zca / phoenix_admin_web_controllers_session_controller.ex
Last active January 19, 2020 15:58
lib/phoenix_admin_web/controllers/session_controller.ex
defmodule PhoenixAdminWeb.SessionController do
use PhoenixAdminWeb, :controller
alias PhoenixAdmin.Accounts
alias PhoenixAdmin.Accounts.User
def new(conn, _) do
changeset = Accounts.change_user(%User{})
conn
@8zca
8zca / phoenix_admin_accounts_user.ex
Created January 19, 2020 14:14
lib/phoenix_admin/accounts/user.ex
# 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
@8zca
8zca / phoenix_admin_accounts_pipeline.ex
Created January 19, 2020 14:13
lib/phoenix_admin/accounts/pipeline.ex
# 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
# 認証ヘッダーの場合の検証
@8zca
8zca / phoenix_admin_web_controller_user_controller.ex
Last active January 19, 2020 15:58
lib/phoenix_admin/web_controller/user_controller.ex
# 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
@8zca
8zca / phoenix_admin_accounts.ex
Created January 13, 2020 06:59
lib/phoenix_admin/accounts.ex
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
@8zca
8zca / phoenix_admin_accounts_error_handler.ex
Last active January 19, 2020 14:15
lib/phoenix_admin/accounts/user.ex
# 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
@8zca
8zca / phoenix_admin_accounts_guardian.ex
Last active January 19, 2020 02:30
lib/phoenix_admin/accounts/guardian.ex
# 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