Skip to content

Instantly share code, notes, and snippets.

@bnhansn
Created October 20, 2016 21:24
Show Gist options
  • Save bnhansn/fc1c989c3427ee2c346fe4cb5083f63c to your computer and use it in GitHub Desktop.
Save bnhansn/fc1c989c3427ee2c346fe4cb5083f63c to your computer and use it in GitHub Desktop.
defmodule Sling.UserController do
use Sling.Web, :controller
alias Sling.User
def create(conn, params) do
changeset = User.registration_changeset(%User{}, params)
case Repo.insert(changeset) do
{:ok, user} ->
new_conn = Guardian.Plug.api_sign_in(conn, user, :access)
jwt = Guardian.Plug.current_token(new_conn)
new_conn
|> put_status(:created)
|> render(Sling.SessionView, "show.json", user: user, jwt: jwt)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Sling.ChangesetView, "error.json", changeset: changeset)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment