auth works now
This commit is contained in:
@ -40,7 +40,8 @@ INSTALLED_APPS = [
|
||||
'rest_framework.authtoken',
|
||||
"rest_framework",
|
||||
"snippets",
|
||||
"custom_auth"
|
||||
"custom_auth",
|
||||
'rest_framework_simplejwt'
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@ -136,3 +137,13 @@ AUTH_PASSWORD_VALIDATORS = {}
|
||||
CORS_ALLOWED_ORIGINS = [
|
||||
"*"
|
||||
]
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
||||
)
|
||||
}
|
||||
|
||||
SIMPLE_JWT = {
|
||||
"TOKEN_OBTAIN_SERIALIZER": "custom_auth.serializers.MyTokenObtainPairSerializer",
|
||||
}
|
||||
|
||||
9
api/app/custom_auth/serializers.py
Normal file
9
api/app/custom_auth/serializers.py
Normal file
@ -0,0 +1,9 @@
|
||||
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||
|
||||
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
|
||||
@classmethod
|
||||
def get_token(cls, user):
|
||||
token = super().get_token(user)
|
||||
token['username'] = user.username
|
||||
return token
|
||||
@ -1,8 +1,16 @@
|
||||
from django.urls import path, include
|
||||
from custom_auth import views
|
||||
from rest_framework_simplejwt.views import (
|
||||
TokenObtainPairView,
|
||||
TokenRefreshView,
|
||||
TokenVerifyView,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path('', include('rest_framework.urls')),
|
||||
path('token/', views.CustomAuthToken.as_view()),
|
||||
path('register/', views.RegisterView.as_view())
|
||||
# path('token/', views.CustomAuthToken.as_view()),
|
||||
path('register/', views.RegisterView.as_view()),
|
||||
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
|
||||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||
path('token/verify/', TokenVerifyView.as_view(), name='token_verify'),
|
||||
]
|
||||
|
||||
@ -4,6 +4,7 @@ from rest_framework.response import Response
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.views import APIView
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
|
||||
class CustomAuthToken(ObtainAuthToken):
|
||||
|
||||
@ -12,12 +13,19 @@ class CustomAuthToken(ObtainAuthToken):
|
||||
context={'request': request})
|
||||
serializer.is_valid(raise_exception=True)
|
||||
user = serializer.validated_data['user']
|
||||
token, created = Token.objects.get_or_create(user=user)
|
||||
return Response({
|
||||
'token': token.key,
|
||||
'user_id': user.pk,
|
||||
'username': user.username
|
||||
})
|
||||
refresh = RefreshToken.for_user(user)
|
||||
|
||||
return {
|
||||
'refresh': str(refresh),
|
||||
'access': str(refresh.access_token),
|
||||
}
|
||||
#
|
||||
# token, created = Token.objects.get_or_create(user=user)
|
||||
# return Response({
|
||||
# 'token': token.key,
|
||||
# 'user_id': user.pk,
|
||||
# 'username': user.username
|
||||
# })
|
||||
|
||||
|
||||
class RegisterView(APIView):
|
||||
@ -45,11 +53,16 @@ class RegisterView(APIView):
|
||||
username=username,
|
||||
password=password
|
||||
)
|
||||
token, created = Token.objects.get_or_create(user=user)
|
||||
return Response({
|
||||
'token': token.key,
|
||||
'user_id': user.pk,
|
||||
'username': user.username
|
||||
}, status=200)
|
||||
refresh = RefreshToken.for_user(user)
|
||||
return {
|
||||
'refresh': str(refresh),
|
||||
'access': str(refresh.access_token),
|
||||
}
|
||||
# token, created = Token.objects.get_or_create(user=user)
|
||||
# return Response({
|
||||
# 'token': token.key,
|
||||
# 'user_id': user.pk,
|
||||
# 'username': user.username
|
||||
# }, status=200)
|
||||
except Exception as e:
|
||||
return Response({"message": "Something went wrong, registration is not completed", "error": e}, status=500)
|
||||
|
||||
Reference in New Issue
Block a user