If you make API calls through AJAX back to the server, most likely you do not want the response redirected to the login page.
I have the same use case and I made my own decorator to return 403 when the user is not logged in. Instead, you can use 401 if you want (I left it commented out).
I use 403 because 401 seems to imply WWW authentication .
from django.http import HttpResponse, HttpResponseForbidden def logged_in_or_deny(func): def check_request(request, *args, **kwargs): if (request.user.is_authenticated()): return func(request, *args, **kwargs) else: return HttpResponseForbidden('You must be logged in')
Then you defend your opinion as follows:
@logged_in_or_deny def your_view(request):
From Angular, it looks like you already know how to use an interceptor to check the response code and redirect the user accordingly. If you use 403 or 401, you should check against the response body in case you respond with similar errors in the future.
While what you already had will work, 302 answers may be used for other reasons. It is better to have an explicit 4xx response rather than a 3xx redirect response, as it will immediately become apparent that this is a client-side error (lack of authentication).
source share