Skip to content

Commit

Permalink
custom token payload
Browse files Browse the repository at this point in the history
  • Loading branch information
pvfarooq committed Sep 4, 2021
1 parent 593cf9b commit 3bc19f8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tfora_social_auth/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.apps import AppConfig


class SocialAuthFarooqConfig(AppConfig):
class TforaSocialAuthConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'tfora_social_auth'
2 changes: 1 addition & 1 deletion tfora_social_auth/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def validate(auth_token):
'/me?fields=name,email,first_name,last_name,middle_name,picture,id')
return profile
except:
return "The token is either invalid or has expired"
return "The token is either invalid or has expired"
8 changes: 4 additions & 4 deletions tfora_social_auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def validate_auth_token(self, auth_token):
user_data = providers.Facebook.validate(auth_token)

try:
user_id = user_data['id']
email = user_data['email']
name = user_data['name']
user_id = user_data.get('id')
email = user_data.get('email')
name = user_data.get('name')
provider = 'facebook'

return register_social_user(
Expand All @@ -59,4 +59,4 @@ def validate_auth_token(self, auth_token):
except Exception as e:
raise serializers.ValidationError(
'The token is invalid or expired. Please login again.'
)
)
3 changes: 2 additions & 1 deletion tfora_social_auth/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.dispatch import Signal, receiver
from .models import User, SocialAccount, SocialApplication
user_registered = Signal()
user_logged_in = Signal()


@receiver(user_registered)
Expand All @@ -10,4 +11,4 @@ def create_social_account(user, provider, provider_data, **kwargs):

if not social_account.exists():
app = SocialApplication.objects.get(provider=provider)
SocialAccount.objects.create(user=user, provider=app)
SocialAccount.objects.create(user=user, provider=app)
59 changes: 45 additions & 14 deletions tfora_social_auth/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from .models import SocialApplication
import random
from .exceptions import CustomException
from rest_framework_simplejwt.tokens import RefreshToken
from . import signals
from .models import SocialApplication
from .exceptions import CustomException
from . models import User, SocialAccount


Expand Down Expand Up @@ -43,48 +42,80 @@ def register_social_user(provider, user_id, email, name, provider_data):
})

filtered_user_by_email = User.objects.filter(email=email)

if filtered_user_by_email.exists():

social_account_user = get_user_social_account(
filtered_user_by_email[0], provider)

if provider == social_account_user.provider.provider:
refresh = RefreshToken.for_user(filtered_user_by_email[0])

signals.user_logged_in.send(
sender="social_user_login",
provider=provider,
user=filtered_user_by_email[0],
provider_data=provider_data
)

token = filtered_user_by_email[0].get_token()

auth_data = {
'refresh': str(refresh),
'access': str(refresh.access_token),
'refresh': str(token),
'access': str(token.access_token),
}

data = {
"status": 200,
"auth": auth_data,
"message": "Logged in successfully"
}

return data
else:

raise CustomException({
'status': 403,
"message": 'Please continue your login using ' + social_account_user.provider.provider
})
else:

if provider == 'google':
first_name = provider_data['given_name']
last_name = provider_data['family_name']

elif provider == 'facebook':
first_name = provider_data['first_name']
last_name = provider_data['last_name']

kwargs = {
'username': generate_username(name),
'email': email,
"is_active": True,
"first_name": first_name,
"last_name": last_name
}

new_user = User.objects.create(**kwargs)
new_user.set_unusable_password()
new_user.save()
signals.user_registered.send(sender="social_user_register",
provider=provider,
user=new_user,
provider_data=provider_data)

refresh = RefreshToken.for_user(new_user)
signals.user_registered.send(
sender="social_user_register",
provider=provider,
user=new_user,
provider_data=provider_data
)

token = new_user.get_token()

auth_data = {
'refresh': str(refresh),
'access': str(refresh.access_token),
'refresh': str(token),
'access': str(token.access_token)
}

data = {
"status": 200,
"auth": auth_data,
"message": "Registered successfully"
}
return data
return data
2 changes: 1 addition & 1 deletion tfora_social_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def post(self, request):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
response = ((serializer.validated_data)['auth_token'])
return Response(response)
return Response(response)

0 comments on commit 3bc19f8

Please sign in to comment.