refactor: moved ExpiringToken to models.py
This commit is contained in:
parent
fee493191e
commit
87f9b3a4b9
2 changed files with 15 additions and 16 deletions
|
|
@ -1,3 +1,15 @@
|
||||||
|
from django.utils import timezone
|
||||||
|
from rest_framework.authtoken.models import Token
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from join.settings import TOKEN_EXPIRATION_TIME
|
||||||
|
|
||||||
# Create your models here.
|
class ExpiringToken(Token):
|
||||||
|
expires_at = models.DateTimeField(null=True, blank=True)
|
||||||
|
|
||||||
|
def is_expired(self):
|
||||||
|
return self.expires_at and self.expires_at < timezone.now()
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
if not self.expires_at:
|
||||||
|
self.expires_at = timezone.now() + TOKEN_EXPIRATION_TIME
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
@ -1,14 +1,12 @@
|
||||||
from django.shortcuts import render, redirect
|
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status, authentication
|
from rest_framework import status, authentication
|
||||||
from rest_framework.authtoken.models import Token
|
|
||||||
from django.contrib.auth import authenticate
|
from django.contrib.auth import authenticate
|
||||||
from join.settings import TOKEN_EXPIRATION_TIME
|
from join.settings import TOKEN_EXPIRATION_TIME
|
||||||
from .serializer import LoginSerializer
|
from .serializer import LoginSerializer
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from django.db import models
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from .models import ExpiringToken
|
||||||
|
|
||||||
|
|
||||||
class LoginView(APIView):
|
class LoginView(APIView):
|
||||||
|
|
@ -72,15 +70,4 @@ class AuthView(APIView):
|
||||||
token.delete()
|
token.delete()
|
||||||
return Response({"error": "Token expired, please log in again"}, status=status.HTTP_401_UNAUTHORIZED)
|
return Response({"error": "Token expired, please log in again"}, status=status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
return Response({"user_id": request.user.id}, status=status.HTTP_200_OK)
|
return Response({"user_id": request.user.id}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
class ExpiringToken(Token):
|
|
||||||
expires_at = models.DateTimeField(null=True, blank=True)
|
|
||||||
|
|
||||||
def is_expired(self):
|
|
||||||
return self.expires_at and self.expires_at < timezone.now()
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
|
||||||
if not self.expires_at:
|
|
||||||
self.expires_at = timezone.now() + TOKEN_EXPIRATION_TIME
|
|
||||||
super().save(*args, **kwargs)
|
|
||||||
Loading…
Reference in a new issue