Description
So i have the "typical" use case of storing the token in Local Storage. Im using a DjangoRest/React front application setup. Now the view below is simple and returns an object. The problem is when I call from the frontend on a MOBILE browser it never authenticates. I have allowed the token to be visible in the html (since I dont know how else to check for this on mobile) for testing purposes, so I know during both mobile and desktop browsersing tokens are being created, being stored correctly, and redux is pickng up the state (retrieving token). Oddly enough if I change to using, a func based view, and calling jwt.decode()
inside of the funciton. The view will properly raise an exception if an incorrect token is passed. Not sure if anyone else is having the same issue or if mine is a duplicate. MAybe the issue is my understanding of model Viewsets? Again, it works in the browser on desktop, I have the settings correct in the settings.py. If i need to post anymore info I will gladly, thanks so much for your help!
desktop only , even with permission_classes commented out. Still fails
class TrackViewSet(viewsets.ModelViewSet):
permission_classes = [
permissions.IsAuthenticated
]
serializer_class = TrackSerializer
def get(self):
print('hi') # does not make it here on mobile
genre = self.request.query_params.get('genre')
track_id = self.request.query_params.get('trackid')
if track_id:
return Track.objects.filter(id=track_id)
if genre == "ALL":
queryset = Track.objects.all()
else:
queryset = Track.objects.filter(genre=genre)
track = random.choice(queryset)
return [track]
Working solution for mobile and desktop. (Please disregard the token sent in POST)
@api_view(['POST'])
def getTracks(request):
token = request.data.get('token')
try:
test = jwt.decode(token, settings.SECRET_KEY, algorithms=['HS256'])
except:
return Response('fail', status=status.HTTP_404_NOT_FOUND)
genre = request.data.get('genre')
track_id = request.data.get('trackid')
if track_id:
return Track.objects.filter(id=track_id)
if genre == "ALL":
queryset = Track.objects.all()
else:
queryset = Track.objects.filter(genre=genre)
track = random.choice(queryset)
new_track = model_to_dict(track)
return Response(new_track)
Urls.py
from django.urls import path, include
from .api import TrackViewSet, FavoriteViewSet, PurchaseViewSet, getTracks
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
# router.register('tracks', TrackViewSet, basename='tracks') # uncomment for 'error code 'endpoint below
router.register('favorites', FavoriteViewSet, basename='favorites')
router.register('purchases', PurchaseViewSet, basename='purchases')
urlpatterns = [
path('', include(router.urls)),
path('getTracks/', getTracks)
]
Not working axios (need to use commented router URLs)
useEffect(() => {
const config = {
headers: {
"Content-type": "application/json",
'Authorization':"tokens.access"
},
};
axios.get('tracks?genre=ALL', config)
.then(res=>setNewTracks(res.data))
.catch(err=>alert('bad',err.response.statusText))
settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',)
}