Skip to content

Instantly share code, notes, and snippets.

@zoltanarvai
Created March 23, 2019 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoltanarvai/df08a2ce08dcddcfb8102a062862a60a to your computer and use it in GitHub Desktop.
Save zoltanarvai/df08a2ce08dcddcfb8102a062862a60a to your computer and use it in GitHub Desktop.
defmodule OrdersWeb.AuthController do
@moduledoc """
This controller allows retrieving an access token from auth0 and returning it to the user
providing username / password based login capability
"""
use OrdersWeb, :controller
alias Auth
alias Auth.{Credentials, TokenResult}
require Logger
# Handles common errors, mainly authorisation and unexpected errors
action_fallback OrdersWeb.FallbackController
def create(conn, credentials) do
_ = Logger.debug(fn -> "Login attempt with user: #{credentials.username}" end)
with {:ok, credentials} <- Credentials.validate(credentials),
{:ok, %TokenResult{} = result} <- Auth.sign_in(credentials) do
conn
|> put_status(:ok)
|> render(:show, token_result: result)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment