Skip to content

Instantly share code, notes, and snippets.

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 joshuataylor/60348be4921994e6f494db89aa110a53 to your computer and use it in GitHub Desktop.
Save joshuataylor/60348be4921994e6f494db89aa110a53 to your computer and use it in GitHub Desktop.
def create(conn, %{"grant_type" => "password",
"username" => username,
"password" => password}) do
try do
user = User
|> where(username: ^username)
|> where(active: true)
|> Repo.one!
cond do
checkpw(password, user.password) ->
# Encode a JWT
{ :ok, jwt, _} = Guardian.encode_and_sign(user, :token)
conn
|> json(%{access_token: jwt}) # Return token to the client
true ->
# Unsuccessful login, return 401
conn
|> put_status(401)
|> render(Admin.ErrorView, "401.json-api")
end
rescue
_ ->
conn
|> put_status(:unprocessable_entity)
|> render(:errors, data: [])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment