Created
June 9, 2016 10:04
-
-
Save ericstumper/08964e1f36b5a1f301d4991d80eee2e3 to your computer and use it in GitHub Desktop.
JWT Token creation with Absinthe and Guardian
This file contains hidden or 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 Languafy.Schema do | |
use Absinthe.Schema | |
import_types Languafy.Schema.Types | |
query do | |
@desc "Get an App User" | |
field :user, type: :user do | |
arg :id, non_null(:id) | |
resolve &Languafy.Resolver.User.find/2 | |
end | |
@desc "Get all courses" | |
field :courses, list_of(:course) do | |
resolve &Languafy.Resolver.Course.all/2 | |
end | |
end | |
mutation do | |
@desc "Create an App User" | |
field :user, type: :user do | |
arg :first_name, non_null(:string) | |
arg :last_name, non_null(:string) | |
arg :email, non_null(:string) | |
arg :password, non_null(:string) | |
resolve &Languafy.Resolver.User.create/2 | |
end | |
@desc "Login User" | |
field :session, :session do | |
arg :token, :string | |
arg :email, non_null(:string) | |
arg :password, non_null(:string) | |
resolve &Languafy.Resolver.Session.create/2 | |
end | |
end | |
end |
This file contains hidden or 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 Languafy.Resolver.Session do | |
alias Languafy.Repo | |
alias Languafy.User | |
def create(args, _info) do | |
user = Repo.get_by(User, email: args[:email]) | |
case authenticate(user, args[:password]) do | |
true -> create_token(user) | |
_ -> {:error, "User could not be authenticated."} | |
end | |
end | |
defp authenticate(user, password) do | |
case user do | |
nil -> false | |
_ -> Comeonin.Bcrypt.checkpw(password, user.password_hash) | |
end | |
end | |
defp create_token(user) do | |
case Guardian.encode_and_sign(user, :token) do | |
nil -> {:error, "An Error occured creating the token"} | |
{:ok, token, full_claims} -> {:ok, token} | |
end | |
end | |
end |
This file contains hidden or 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 Languafy.Schema.Types do | |
use Absinthe.Schema.Notation | |
@desc "An App User" | |
object :user do | |
field :id, :id | |
field :first_name, :string | |
field :last_name, :string | |
field :email, :string | |
field :courses, list_of(:course) | |
end | |
@desc "A Languafy Course" | |
object :course do | |
field :id, :id | |
field :title, :string | |
field :description, :string | |
field :users, list_of(:user) | |
end | |
@desc "Session Data" | |
object :session do | |
field :token, :string | |
field :email, :string | |
field :password, :string | |
end | |
@desc "A Token" | |
object :token do | |
field :token, :string | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment