0

in my Django app i need to provide rest api's for almost all the views. so what i need to do is provide login option by using an api.

The default behaviour of auth_views.login is to redirect after login to a redirect url specified, however i need to check if content-type is application/json in the request i need to return the session id instead of redirecting to the redirect url & then the user can use this session id to use other api's?

how could i do that. remember i cannot usedjango-rest-framework

anekix
  • 2,393
  • 2
  • 30
  • 57
  • This post might help to detect [content type](https://stackoverflow.com/questions/7459881/determine-the-requested-content-type), and handle accordingly. A decorator which calls this validation can be used. – Vinay P Jul 16 '18 at 11:20
  • @VinayP so what i was thinkin is to use inbuil `auth_views.login` and use a custom middleware to detetc the ontent type and based on it set an an attribute for ex `reuires_json_response=True` check for this attribute in my view and return result accordingly? does it makes sense? i am very very new to django world – anekix Jul 16 '18 at 11:23
  • yes seems a good way too. – Vinay P Jul 16 '18 at 11:25
  • @VinayP but there is a problem in this . i am using `auth_view.login`. but according to my solution, it requires to be able to change this view for looking `content type` and then returning `session id`. how can i go about this.? so in short question is how can i make my login api to rest ? all other apis i can do as i have acces to them – anekix Jul 16 '18 at 11:28
  • You have to override the View, and start using the new view for the logins. Changing django built in login API is not a good idea. – Vinay P Jul 16 '18 at 11:34
  • @VinayP do you mean creating new view for login and mapping my `login/` url to my custom login view? and forget `about django.contrib.auth`? i guess there should be some other way as this would mean missing out so many security features already implemented by buil-in login view – anekix Jul 16 '18 at 11:36
  • I guess i can just decorate the inbuilt login view to get the desired effect – anekix Jul 16 '18 at 11:39
  • There is facility to change the default Auth class used in django. – Vinay P Jul 16 '18 at 11:40
  • how any doc on that? – anekix Jul 16 '18 at 11:40
  • https://docs.djangoproject.com/en/2.0/topics/auth/customizing/ – Vinay P Jul 16 '18 at 11:41
  • not sure if this will override auth view, just a pointer from my side. – Vinay P Jul 16 '18 at 11:43
  • i have looked in the doc above but it shows only using custom backends & extending User model but what i need is different. – anekix Jul 16 '18 at 11:44

0 Answers0