Android with Django: How to keep user logged in

心不动则不痛 提交于 2019-12-04 18:19:55

I don't think you should store user credentials in preferences.

The most common approach is to send credentials to server and then as a response get a session key. Then include the session key as a header to any request (and validate it in every request).

If the session key would become invalid (e. g. expired) then the server should return a proper response, and the client should initialize authentication functionality.

Egzample

First run

  1. Show login Activity
  2. Send credentials to server
  3. Get a session_key as a response (normally its a hash)
  4. Store the session_key hash
  5. User is authenticated, exit the login Activity

Any request to the server.

  1. Add a header with session_key to your request (e. g. as a header)
  2. Send the request
  3. If the response is Ok Stop, else (e. g. response with message "not authorized" or status code 401) run First run

First run

  • Show login Activity
  • Send credentials to server
  • Get a session_key
  • User is authenticated, exit the login Activity
  • you get the access tokens and login details to stored in Shared Preferences

For other request.

  • Add a header with session_key to your request (e. g. as a header)
  • Send the request
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!