Skip to content

Commit dafefb7

Browse files
committed
Test Authentication
1 parent 82b651b commit dafefb7

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

hackernews/users/schema.py

+7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ class Meta:
1111

1212
class Query(graphene.ObjectType):
1313
users = graphene.List(UserType)
14+
me = graphene.Field(UserType)
1415

1516
def resolve_users(self, info):
1617
return get_user_model().objects.all()
1718

19+
def resolve_me(self, info):
20+
user = info.context.user
21+
if user.is_anonymous:
22+
raise Exception("Not logged in!")
23+
return user
24+
1825

1926
class CreateUser(graphene.Mutation):
2027
user = graphene.Field(UserType)

queries.curl

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ curl -s -X POST -H "Content-Type: application/json" -d '{"query":"query{users{id
44
curl -s -X POST -H "Content-Type: application/json" -d '{"query":"mutation{createUser(username: \"dsl\", email: \"none\", password: \"123\"){user{id, email, password}}}"}' http://localhost:8000/graphql/
55
curl -s -X POST -H "Content-Type: application/json" -d '{"query":"mutation{tokenAuth(username: \"zsl\", password: \"123\"){token}}"}' http://localhost:8000/graphql/
66
curl -s -X POST -H "Content-Type: application/json" -d '{"query":"mutation{verifyToken(token: \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InpzbCIsImV4cCI6MTYyODE1NDg2Niwib3JpZ19pYXQiOjE2MjgxNTQ1NjZ9.x8ecGqD7Ple39GM_YMrqnmwaoLFsXiJnB4x9tm0zsjk\"){payload}}"}' http://localhost:8000/graphql/
7+
curl -s -X POST -H "Content-Type: application/json" -H "Authorization: JWT `cat token`" -d '{"query":"query{me{username email}}"}' http://localhost:8000/graphql/

0 commit comments

Comments
 (0)