Python Social Auth support for Django GraphQL
- Python ≥ 3.4
- Django ≥ 1.11
Install last stable version from Pypi.
pip install django-graphql-social-auth
See the documentation for further guidance on setting Python Social Auth.
Add the SocialAuthComplete
mutation to your GraphQL schema.
import graphene
import graphql_social_auth
class Mutations(graphene.ObjectType):
social_auth = graphql_social_auth.SocialAuthComplete.Field()
Session authentication via accessToken.
provider
: provider name from Authentication backend list.accessToken
: third-party (Google, Facebook...) OAuth token obtained with any OAuth client.
mutation SocialAuthComplete($provider: String!, $providerData: JSONString!) {
socialAuthComplete(provider: $provider, providerData: $providerData) {
result {
__typename
... on Social {
social {
uid
extraData
}
isSuccessfulLogin
isInactiveUser
isNew
isNewAssociation
}
}
}
}
Authentication solution based on JSON Web Token.
Install additional requirements.
pip install 'django-graphql-social-auth[jwt]'
Add the SocialAuthJWTComplete
mutation to your GraphQL schema.
import graphene
import graphql_social_auth
class Mutations(graphene.ObjectType):
social_auth_complete = graphql_social_auth.SocialAuthJWTComplete.Field()
Authenticate via accessToken to obtain a JSON Web Token.
mutation SocialAuthComplete($provider: String!, $providerData: JSONString!) {
socialAuthComplete(provider: $provider, providerData: $providerData) {
result {
__typename
... on JWT {
social {
uid
extraData
}
token
isSuccessfulLogin
isInactiveUser
isNew
isNewAssociation
}
}
}
}
Complete support for Relay.
import graphene
import graphql_social_auth
class Mutations(graphene.ObjectType):
social_auth_complete = graphql_social_auth.relay.SocialAuthComplete.Field()
graphql_social_auth.relay.SocialAuthJWTComplete.Field()
for JSON Web Token (JWT) authentication.
Relay mutations only accepts one argument named input:
mutation SocialAuthComplete($provider: String!, $providerData: JSONString!) {
socialAuthComplete(input:{provider: $provider, providerData: $providerData}) {
result {
__typename
... on Social {
social {
uid
extraData
}
isSuccessfulLogin
isInactiveUser
isNew
isNewAssociation
}
}
}
}
If you want to customize the SocialAuthComplete
behavior, you'll need to customize the get_result()
method on a subclass of SocialAuthComplete
and add a new .relay.SocialAuthComplete
for relay.
import graphene
from graphql_social_auth import mutations, results
class UserSocial(results.Social):
user = graphene.Field(UserType)
class SocialAuthCompleteResult(graphene.Union):
class Meta:
types = [UserSocial, results.Redirect, results.Html]
class SocialAuthComplete(mutations.SocialAuthCompleteMutation):
result = graphene.Field(SocialAuthCompleteResult)
@classmethod
def get_result(cls,
backend,
user,
is_successful_login,
is_inactive_user,
is_new,
is_new_association,
**kwargs):
return UserSocial(user=user,
social=user.social_user,
is_successful_login = is_successful_login,
is_inactive_user = is_inactive_user,
is_new = is_new,
is_new_association = is_new_association,
session = backend.strategy.session)
Authenticate via accessToken to obtain the user id.
mutation SocialAuthComplete($provider: String!, $providerData: JSONString!) {
socialAuthComplete(provider: $provider, providerData: $providerData) {
result {
__typename
... on UserSocial {
social {
uid
extraData
}
user {
id
}
isSuccessfulLogin
isInactiveUser
isNew
isNewAssociation
}
}
}
}
Gracias @omab / Python Social Auth.