# Preface
This tutorial is based on the "apifm-wxapi" module and teaches you how to quickly implement small program development, so you may need to understand the following knowledge points first:
["Creating HelloWorld Project"](https://www.yuque.com/apifm/doc/hezlrm)
["Use "apifm-wxapi" to quickly develop small programs"](https://www.yuque.com/apifm/doc/mdldsd)
["Register for free to open the backend and obtain an exclusive domain name"](https://www.yuque.com/apifm/doc/qr6l4m)
# Requirements description
Use the wx.login() method of the mini program to obtain the temporary code;
Use temporary code to exchange for user login token through "apifm-wxapi";
Any authorized login by the user in the future will require this token to operate;
According to the official documentation of the mini program, for security reasons, any sensitive information (such as appid, secret, accesstoken, etc.) should not be exposed on the mini program side, and try to avoid directly using the session_key for mini program login;
# budget
No cost, free implementation
# Requirements realization
### Preparation
Because this example is for mini program development, you must configure your mini program appid and secret in the background before performing the following work;
Because only if the correct mini program appid and secret are configured in the background, the background can correctly parse the encrypted user data submitted by the mini program to the api interface, and then the user login can be completed.
["Appid and secret of background configuration applet"](https://www.yuque.com/apifm/doc/ngdgyi)
**If you have not registered a user, calling this example to log in will return you an error that the user does not exist! **
_You can go to the mini program user registration section, complete the user registration first, and then log in to the mini program: _
["Mini program user registration, the background can view and manage registered users"](https://www.yuque.com/apifm/doc/ryxim1)
### Write small program code:
```plain
const WXAPI = require('apifm-wxapi')
WXAPI.init('gooking')
Page({
data: {
},
onLoad: function (options) {
},
login(e){
if (!e.detail.userInfo) {
// You clicked to cancel authorization
return;
}
wx.login({
success: function (res) {
const code = res.code; // The code parameter returned by the WeChat login interface is needed for the following login interface.
WXAPI.login_wx(code).then(function (res) {
// Login interface returns results
console.log(res)
})
}
})
}
})
```
> WXAPI.init('gooking') This code links your mini program to your backend, where gooking is your exclusive domain name (please check the chapter about exclusive domain names in the preface);
>
_After successful login, you will be able to obtain the uid, openid, and token of the current user_
**Token is recommended to be saved directly in the storage of the mini program. In the future, whenever it involves functions that require users to log in before they can be operated, just pass this token directly! **
# Troubleshooting
["Registration/login failed, user openid cannot be obtained"](https://www.yuque.com/apifm/doc/qgzm02)
Looking forward to your progress!
Thanks!