# -*- coding: utf-8 -*- # # ITerativ GmbH # http://www.iterativ.ch/ # # Copyright (c) 2019 ITerativ GmbH. All rights reserved. # # Created on 2019-10-01 # @author: chrigu import re import graphene from django.contrib.auth import authenticate, login from graphene import relay class FieldError(graphene.ObjectType): code = graphene.String() class MutationError(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(MutationError) # 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()