Using Sessions vs Tokens for API authentication

前端 未结 4 1709
情深已故
情深已故 2020-12-23 08:04

I have built a simple test API for a CakePHP application that will let a user login from a mobile device (or any device for that matter) and get a JSON response. This API co

4条回答
  •  被撕碎了的回忆
    2020-12-23 09:04

    Make your app login everytime, but not with login-pass pair as Swayok lastly suggested. When you login, server generates a token and returns it back to the client. Client then uses this token whenever it makes a request. On each request, server checks whether the token is valid and if so, executes the request.

    This is very similar to how sessions work in that, server side frameworks manage it internally and these tokens expire from time to time. However, as Swayok rightuflly pointed out, you don't want session mainly because you're RESTful API should have no state. You get the same utility without storing any user specific data regarding user and logging user in with every request.

    Here's a good article on this, or you can try the Facebook Graph API explorer to see it in action

提交回复
热议问题