Joke Collection Website - Public benefit messages - How to implement the WeChat applet to exit the account?
How to implement the WeChat applet to exit the account?
Therefore, the specific process for the WeChat applet to exit the account includes the following points:
1. User login operation:
(1) WeChat applet calls wx.login to get the login certificate (code).
(2) Pass the code to the back-end service, then exchange the login status information of the user, including the unique identifier (openid) of the user, and generate a token value corresponding to the user one by one, which is saved and returned to the WeChat applet client.
(3) The WeChat applet obtains the token and saves it locally, which can be used as a certificate of login status.
The front-end code is:
const login =? Function () {
wx . show loading({ title:‘landing‘})
wx.login()。 Then (res =>{
if(RES . errmsg = = =‘log in:ok‘){
API . log in(RES . code)。 Then (data =>{
wx.hideLoading()
wx . setstoragesync(‘token‘,data)})
}})}
The backend service code is:
Asynchronous login (jsCode ){
const { ctx,app } = this
const APPID = app . config . mini program . APPID;
const SECRET = app . config . mini program . app SECRET;
const url = `/sns/jscode2session? appid = $ { APPID } & ampsecret = $ { SECRET } & ampjs _ code = $ { jsCode } & ampgrant _ type = authorization _ code `;
const RES = await CTX . curl(URL,{ method:‘GET‘,dataType:‘JSON‘});
const OpenID = RES . data . OpenID;
const session _ key = RES . data . session _ key;
const str = await app . redis . get(OpenID);
if(str & amp; & ampJSON . parse(str)。 Access token){// logged in.
const oldAccessToken = JSON . parse(str)。 accessToken
await app . redis . del(oldAccessToken); //Delete the token to realize single sign-on.
await app . redis . del(OpenID); }
const access token = await CTX . helper . createwxtoken(OpenID,session _ key); //Generate a new token
Return accessToken}
2. Exit the login operation:
(1) Click the "Exit Account" button to call ajax to request the backend to exit the login interface and pass the user token. If the exit is successful, the login status and token information of the current user will be cleared.
(2) Refresh the current page data and clear the user information on the page.
(3) After receiving the user token passed by the client, the back-end API queries the database to determine whether the user has logged in, and if so, deletes the token or sets it as invalid.
The front-end code is:
const logout =? Function () {
Wx.showloading ({title:' logout'})
const token = wx . getstoragesync(‘token‘)
API . logout({ token:token })。 Then (data =>{
wx.hideLoading()
wx . removestoragesync(‘token‘)
refreshPageData()
})}
The back-end code is:
Asynchronous logout (access token ){
const { ctx,app } = this
const userId = await app . redis . get(access token);
If (! UserId)CTX. throw(400, "Login status expired");
await app . redis . del(userId); //Delete User ID
await app . redis . del(access token); //Delete the token
}
Extended data:
After password login and authorized login of 1. wechat applet, the token will be saved in loacalStore. Every time the interface is loaded, it will carry a token to convey the login status to the server.
2. The login status needs to be cleared when the WeChat applet logs off. When calling wx.login () in the WeChat applet API for login operation, you need to get the code through the login interface and then call the back-end interface to generate token. For security reasons, do not use key generation in the front end. When logging off, you need to clear the cache token and user information in localStorage and refresh the current page data.
- Related articles
- Has the admission notice of Anqing Normal University been issued?
- Thank the teacher for his composition on the topic.
- How to prohibit Taobao shops from sending short messages?
- Will the plaintiff be notified if the judgment has been withdrawn?
- What is the complaint telephone number of Tianjin Unicom?
- Those who just do what they say...wine copywriters
- I got a text message from Bank of Ningbo for no reason.
- What should I do if I encounter domestic violence?
- How to complain about the title of telecom anti-fraud for no reason
- Best birthday wishes