Before, my session
was modeled on the source code of cnodejs
, using express-session
and cookie
to obtain information.
Now I want to add a login interface for the WeChat applet and maintain the login status. Mini programs cannot use cookie
WeChat team provides a weapp-sesion
I would like to ask everyone, how should I change it now? To be suitable for both
大家讲道理2017-05-16 13:23:21
If you are familiar with the principle of http session, the session problem can be easily solved. A common way to maintain a session is that when the browser initiates an http request to the server, the server checks whether the session id is included in the http header cookie parameter. If there is a session id, it checks the session stored on the server side based on the session id. The session is saved in the session. Some information about the current session. If the sessionid does not exist, the server will assign one and write it into the cookie field, and the browser will bring it with it the next time it initiates another request. All requests in the mini program are initiated through the wx.request API. If you wrap the wx.request API so that every time it makes a request to the server, it also adds an http header named Cookie, so there is no need to make changes to the server. The sessionid assigned by the server is stored in the WeChat client using the wx.setStorageSync API.
http://www.wxapp-union.com/ar...
That is to say, the client WeChat applet solves the problem of storing sessionid. The session function implemented by nodejs on the server side is no different from the ordinary session implementation. In the mini program, send the sessionid together every time you request your server. On the server, you first determine whether there is the sessionid parameter you sent. If there is, continue to operate the session. If not, assign a sessionid and send it together with the response. Give the client a WeChat applet. Every time a response is received in the applet, it will be judged whether there is the sessionid you set. If so, it will be stored locally and sent together with the next request.
Usually, the cookie in our browser only stores the sessionid and sends it to the server to identify the user. Of course, every time the mini program provides a session, we can use the wx.setStorageSync API to store it in the WeChat client instead of the cookie.