Add login endpoint
This commit is contained in:
parent
e982579711
commit
7d6a03743c
|
|
@ -5,7 +5,7 @@ from graphene_django.debug import DjangoDebug
|
||||||
|
|
||||||
|
|
||||||
from users.schema_public import UsersQuery
|
from users.schema_public import UsersQuery
|
||||||
from users.mutations import ProfileMutations
|
from users.mutations_public import UserMutations
|
||||||
|
|
||||||
|
|
||||||
class Query(UsersQuery, graphene.ObjectType):
|
class Query(UsersQuery, graphene.ObjectType):
|
||||||
|
|
@ -15,11 +15,10 @@ class Query(UsersQuery, graphene.ObjectType):
|
||||||
debug = graphene.Field(DjangoDebug, name='__debug')
|
debug = graphene.Field(DjangoDebug, name='__debug')
|
||||||
|
|
||||||
|
|
||||||
# class Mutation(BookMutations, RoomMutations, AssignmentMutations, ObjectiveMutations, CoreMutations, PortfolioMutations,
|
class Mutation(UserMutations, graphene.ObjectType):
|
||||||
# ProfileMutations, SurveysMutations, graphene.ObjectType):
|
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
debug = graphene.Field(DjangoDebug, name='__debug')
|
debug = graphene.Field(DjangoDebug, name='__debug')
|
||||||
|
|
||||||
|
|
||||||
schema = graphene.Schema(query=Query)
|
schema = graphene.Schema(query=Query, mutation=Mutation)
|
||||||
|
|
@ -3,7 +3,7 @@ from django.conf.urls import url
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from graphene_django.views import GraphQLView
|
from graphene_django.views import GraphQLView
|
||||||
|
|
||||||
from api.public_schema import schema
|
from api.schema_public import schema
|
||||||
|
|
||||||
from core.views import PrivateGraphQLView
|
from core.views import PrivateGraphQLView
|
||||||
|
|
||||||
|
|
@ -15,7 +15,7 @@ urlpatterns = [
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
urlpatterns += [url(r'^graphiql-public', csrf_exempt(GraphQLView.as_view(schema=schema, graphiql=True,
|
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)))]
|
urlpatterns += [url(r'^graphiql', csrf_exempt(PrivateGraphQLView.as_view(graphiql=True, pretty=True)))]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue