Skip to content

Commit

Permalink
Merge pull request #7 from L3-iGrant/feature/2-buyer-login-api
Browse files Browse the repository at this point in the history
Add #2 Buyer login API
  • Loading branch information
georgepadayatti authored Nov 2, 2022
2 parents 9c6accc + b4e0dc3 commit a218a68
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions igrant_user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion igrant_user/urls.py
Original file line number Diff line number Diff line change
@@ -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('/<int:pk>/', UserDetail.as_view()),
path('/login/', MyTokenObtainPairView.as_view(), name='token_obtain_pair'),
path('/api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]
17 changes: 17 additions & 0 deletions igrant_user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
1 change: 1 addition & 0 deletions pob_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework_simplejwt.authentication.JWTAuthentication",
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
],
Expand Down
1 change: 0 additions & 1 deletion pob_backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down

0 comments on commit a218a68

Please sign in to comment.