Add login endpoint

This commit is contained in:
Christian Cueni 2019-10-01 15:07:36 +02:00
parent e982579711
commit 7d6a03743c
3 changed files with 51 additions and 6 deletions

View File

@ -5,7 +5,7 @@ from graphene_django.debug import DjangoDebug
from users.schema_public import UsersQuery
from users.mutations import ProfileMutations
from users.mutations_public import UserMutations
class Query(UsersQuery, graphene.ObjectType):
@ -15,11 +15,10 @@ class Query(UsersQuery, graphene.ObjectType):
debug = graphene.Field(DjangoDebug, name='__debug')
# class Mutation(BookMutations, RoomMutations, AssignmentMutations, ObjectiveMutations, CoreMutations, PortfolioMutations,
# ProfileMutations, SurveysMutations, graphene.ObjectType):
class Mutation(UserMutations, graphene.ObjectType):
if settings.DEBUG:
debug = graphene.Field(DjangoDebug, name='__debug')
schema = graphene.Schema(query=Query)
schema = graphene.Schema(query=Query, mutation=Mutation)

View File

@ -3,7 +3,7 @@ from django.conf.urls import url
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView
from api.public_schema import schema
from api.schema_public import schema
from core.views import PrivateGraphQLView
@ -15,7 +15,7 @@ urlpatterns = [
if settings.DEBUG:
urlpatterns += [url(r'^graphiql-public', csrf_exempt(GraphQLView.as_view(schema=schema, graphiql=True,
pretty=True)))]
pretty=True)))]
urlpatterns += [url(r'^graphiql', csrf_exempt(PrivateGraphQLView.as_view(graphiql=True, pretty=True)))]

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
#
# Created on 2019-10-01
# @author: chrigu <christian.cueni@iterativ.ch>
import graphene
from django.contrib.auth import authenticate, login
from graphene import relay
class FieldError(graphene.ObjectType):
code = graphene.String()
class UpdateError(graphene.ObjectType):
field = graphene.String()
errors = graphene.List(FieldError)
class Login(relay.ClientIDMutation):
class Input:
username_input = graphene.String()
password_input = graphene.String()
success = graphene.Boolean()
errors = graphene.List(UpdateError) # todo: change for consistency
@classmethod
def mutate_and_get_payload(cls, root, info, **kwargs):
user = authenticate(username=kwargs.get('username_input'), password=kwargs.get('password_input'))
if user is not None:
login(info.context, user)
return cls(success=True, errors=[])
else:
return cls(success=False, errors=['invalid_credentials'])
class UserMutations:
login = Login.Field()