from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions
from rest_framework.authtoken.models import Token
import time
from api.models import User

class TokenAuthentication(BaseAuthentication):
    def authenticate(self, request):
        try:
            token = request.headers['Authorization'].replace("Token ", "")
            if Token.objects.filter(key=token).exists():
                user_id = Token.objects.filter(key=token).first().user_id
                user= User.objects.get(id=user_id)
                return (user, token)
            return None
            
            
        except Exception as error:
            print(error)
            raise exceptions.AuthenticationFailed("Failed pass authenticate")