Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF: How to password protect the DefaultRouter

How would you go about protecting DefaultRouter() so the user can not view API Root unless they are logged in?

class OrderViewSet(viewsets.ReadOnlyModelViewSet):
    permission_classes = (permissions.IsAuthenticated)
    queryset = Order.objects.all()
    serializer_class = OrderSerializer

router = routers.DefaultRouter()
router.register(r'orders', views.OrderViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^auth/', include('rest_framework.urls')),
]  

project.urls.py

# API
url(r'^api/', include(api_urls)),
like image 337
bdoubleu Avatar asked Apr 11 '18 10:04

bdoubleu


1 Answers

You can add default permission classes in your settings.py file:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    )
}
like image 166
Harsh Avatar answered Nov 12 '22 17:11

Harsh