Created
March 23, 2019 11:30
-
-
Save zoltanarvai/df08a2ce08dcddcfb8102a062862a60a to your computer and use it in GitHub Desktop.
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 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