forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.py
More file actions
51 lines (40 loc) · 1.58 KB
/
authenticate.py
File metadata and controls
51 lines (40 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import graphene
from functions.input_validators import cleanse_input
from schemas.shared_structures import AuthResult
from schemas.authenticate.sign_in_user import sign_in_user
from scalars.email_address import EmailAddress
class AuthenticateInput(graphene.InputObjectType):
"""
Object containing input fields for Authenticate Mutation
"""
user_name = EmailAddress(
description="User email that they signed up with.", required=True,
)
password = graphene.String(description="Users password", required=True,)
class Authenticate(graphene.Mutation):
"""
This mutation allows users to give their credentials and retrieve a token
that gives them access to restricted content.
"""
# Define mutation arguments
class Arguments:
input = AuthenticateInput(
required=True,
description="AuthenticateInput object containing input fields",
)
# Define mutation fields
auth_result = graphene.Field(
lambda: AuthResult, description="User info who just signed in, and their JWT"
)
# Define mutation functionality
@staticmethod
def mutate(self, info, **kwargs):
# Get arguments
user_name = cleanse_input(kwargs.get("input", {}).get("user_name"))
password = cleanse_input(kwargs.get("input", {}).get("password"))
# Create user and JWT
user_info = sign_in_user(user_name=user_name, password=password)
# Return information to user
return Authenticate(
AuthResult(str(user_info.get("auth_token")), user_info.get("user"))
)