User Authorization

Some interfaces require user authorization to call. We divide these interfaces into multiple scope ,according to the scope of use. When user chooses to authorize scope ,after authorizing a scope, all corresponding interfaces can be used directly.

Call The Interface to Initiate Authorization

When using the interface under a scope for the first time, a pop-up window will ask the user, "XXX is applying for the following authorizations: * (privilege description)*". If the user clicks Allow, the interface permissions for this scope can be obtained. And calling the interface is successful, otherwise it fails.

wx.login({
  success: function () {
    wx.getUserInfo()
  }
})

Initiate authorization in advance

If you need to initiate authorization in advance to get the user's consent, you can call wx.authorize() to initiate the authorization in advance.

wx.authorize({
  scope: 'scope.record'
})

Handle User’s Rejecting Authorization

The user may reject the authorization request initiated by the Mini Program and need to handle this situation.

wx.login({
  success: function () {
    wx.getUserInfo({
      fail: function (res) {
        // Callback errMsg of iOS and Android for rejecting authorization is not unified, which requires some compatibility processing
        if (res.errMsg.indexOf('auth deny') > -1 || 	res.errMsg.indexOf('auth denied') > -1 ) {
          // Handle user’s rejecting authorization
        }
      }
    })
  }
})

wx.authorize({
  scope: 'scope.record',
  fail: function (res) {
    // Callback errMsg of iOS and Android for rejecting authorization is not unified, which requires some compatibility processing
    if (res.errMsg.indexOf('auth deny') > -1 || 	res.errMsg.indexOf('auth denied') > -1 ) {
      // Handle user’s rejecting authorization
    }    
  }
})

Get User Authorization Settings

The user's current authorization processing information can be obtained by calling the wx.getSetting() interface.

wx.getSetting({
  success: function (res) {
    var authSetting = res.authSetting
    if (authSetting['scope.userInfo'] === true) {
      // The user has authorized and can call the relevant API directly
    } else if (authSetting['scope.userInfo'] === false){
      // The user has rejected the authorization, and the calling of relevant API or wx.authorize will fail. You need to guide the user to the settings page and open the authorization switch.
    } else {
      // When the user has not been asked for authorization yet, calling the relevant API or wx.authorize will pop up the window to ask the user
    }
  }
})

Guide Users to Reauthorize

If the user rejected a scope authorization request, subsequent API calls under scope will fail directly, using wx.authorize() to apply for this scope will also fail directly without asking the user by pop-ups. In this case, you need to guide the user to go to the Settings page to open the corresponding scope permission.

The entry path to the authorization page is: top right menu -> About (Mini Program name) -> top right menu -> Settings

Note: Only the scope that has applied for authorization will appear on the Settings page.

Reset Authorization Record

If the user deletes the current Mini Program from the list, all authorized and rejected authorization records are cleared.

Scope List

scope corresponding API description
scope.userInfo wx.getUserInfo() User Information
scope.userLocation wx.getLocation() Geographical Position
scope.werun wx.getWeRunData() WeRun Steps
scope.record wx.startRecord(), RecorderManager.start() Recording Function
scope.writePhotosAlbum wx.saveImageToPhotosAlbum() Save to Album