From b4e0dc33a9c20cfe7894a1448de3e563f828abae Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Wed, 2 Nov 2022 17:27:44 +0530 Subject: [PATCH] Add #2 Buyer login API Method POST Endpoint /users/login --- igrant_user/models.py | 2 ++ igrant_user/urls.py | 5 ++++- igrant_user/views.py | 17 +++++++++++++++++ pob_backend/settings.py | 1 + pob_backend/urls.py | 1 - 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/igrant_user/models.py b/igrant_user/models.py index 1cd2098..4857c5c 100644 --- a/igrant_user/models.py +++ b/igrant_user/models.py @@ -10,6 +10,8 @@ class IGrantUser(AbstractBaseUser, PermissionsMixin): class UserType(models.TextChoices): COMPANY = "COMPANY", _("Company") ISSUER = "ISSUER", _("Issuer") + BUYER = "BUYER", _("Buyer") + SELLER = "SELLER", _("Seller") class Orgs(models.TextChoices): DEFAULT = "NIL", _("Nil") diff --git a/igrant_user/urls.py b/igrant_user/urls.py index 3df72ea..f17074d 100644 --- a/igrant_user/urls.py +++ b/igrant_user/urls.py @@ -1,8 +1,11 @@ from django.contrib import admin from django.urls import path, include -from .views import UserList, UserDetail +from .views import UserList, UserDetail, MyTokenObtainPairView +from rest_framework_simplejwt.views import TokenRefreshView urlpatterns = [ path('/', UserList.as_view()), path('//', UserDetail.as_view()), + path('/login/', MyTokenObtainPairView.as_view(), name='token_obtain_pair'), + path('/api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), ] diff --git a/igrant_user/views.py b/igrant_user/views.py index e58b249..d5f5902 100644 --- a/igrant_user/views.py +++ b/igrant_user/views.py @@ -3,6 +3,8 @@ from .serializers import IGrantUserSerializer from rest_framework import permissions from .permissions import IsOwnerOrReadOnly +from rest_framework_simplejwt.serializers import TokenObtainPairSerializer +from rest_framework_simplejwt.views import TokenObtainPairView # Create your views here. @@ -19,3 +21,18 @@ class UserDetail(generics.RetrieveAPIView): queryset = IGrantUser.objects.all() serializer_class = IGrantUserSerializer permission_classes = [permissions.IsAuthenticated, IsOwnerOrReadOnly] + + +class MyTokenObtainPairSerializer(TokenObtainPairSerializer): + @classmethod + def get_token(cls, user): + token = super().get_token(user) + + # Add custom claims + token['email'] = user.email + token['user_type'] = user.user_type + + return token + +class MyTokenObtainPairView(TokenObtainPairView): + serializer_class = MyTokenObtainPairSerializer \ No newline at end of file diff --git a/pob_backend/settings.py b/pob_backend/settings.py index a77a5a4..60ac362 100644 --- a/pob_backend/settings.py +++ b/pob_backend/settings.py @@ -133,6 +133,7 @@ REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": [ + "rest_framework_simplejwt.authentication.JWTAuthentication", "rest_framework.authentication.SessionAuthentication", "rest_framework.authentication.TokenAuthentication", ], diff --git a/pob_backend/urls.py b/pob_backend/urls.py index 4f722b0..77b585a 100644 --- a/pob_backend/urls.py +++ b/pob_backend/urls.py @@ -18,7 +18,6 @@ urlpatterns = [ path("admin/", admin.site.urls), - path(r"rest-auth/", include("rest_auth.urls")), path(r"users", include("igrant_user.urls")), path(r"connections", include("connections.urls")), path(r"certificates", include("certificate.urls")),