Nithin
Nithin

Reputation: 5840

Remove user endpoints in Django rest auth

I am using Django rest auth for user account handling. For updating user info i have created custom endpoints, So i don't need the djnago-rest-auth generated endpoints /rest-auth/user/ (GET, PUT, PATCH). How can i remove these endpoints?

urls.py

urlpatterns = [
    path('', include("rest_auth.urls"), name="user-auth"),
    path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
    path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]

Edit

I want to use all other urls of rest-auth like login, register, etc. But i just dont want the /rest-auth/user/ as described here.

Upvotes: 1

Views: 1465

Answers (3)

Igor Jerosimić
Igor Jerosimić

Reputation: 13741

I've created a helper function for this:

def include_filter(arg, namespace=None, exclude: list | None = None):
    """Include urls and exclude some of them by pattern name."""
    data = include(arg, namespace)

    if exclude:
        for pattern in data[0].urlpatterns:
            if pattern.name in exclude:
                data[0].urlpatterns.remove(pattern)

    return data

Then in your API urls sections you call it like this:

path('account/', include_filter("dj_rest_auth.urls", exclude=["rest_user_details"])),

Upvotes: 0

Nithin
Nithin

Reputation: 5840

@bodoubleu 's answer didn't work, So i added them manually.

from rest_auth.views import (
    LoginView, LogoutView, PasswordChangeView,
    PasswordResetView, PasswordResetConfirmView
)

urlpatterns = [
    path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
    path('login', LoginView.as_view(), name="user-login"),
    path('logout', LogoutView.as_view(), name='user-logout'),
    path('password/change/', PasswordChangeView.as_view(), name='rest_password_change'),
    path('password/reset', PasswordResetView.as_view(), name='rest_password_reset'),
    path('password/reset/confirm/', PasswordResetConfirmView.as_view(), name='rest_password_reset_confirm'),
    path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]

Upvotes: 4

bdoubleu
bdoubleu

Reputation: 6107

Untested but this should work.

urlpatterns = [
    path('user/', django.views.defaults.page_not_found),
    path('', include("rest_auth.urls"), name="user-auth"),
    path('register', include('rest_auth.registration.urls'), name="user-auth-registration"),
    path('<uid>/', views.UserProfileView.as_view(), name="user-profile"),
]

If not you can manually define all the rest_auth.urls in your url patterns

Upvotes: 0

Related Questions