# Scan code login module
This module is used for scanning code login, which is divided into two types of small program scanning code login (wxqrcode) and APP scanning code login (qrcode). The API of the two scan code login methods is exactly the same.
Use the mini program to scan the code to log in:
import {AuthenticationClient} from "authing-js-sdk"
const authenticationClient = new AuthenticationClient({
appId: "YOUR_APP_ID",
})
authenticationClient.wxqrcode.startScanning() # Start scan code login
Use APP scan code to log in
import {AuthenticationClient} from "authing-js-sdk"
const authenticationClient = new AuthenticationClient({
appId: "YOUR_APP_ID",
})
authenticationClient.qrcode.startScanning() # Start scan code login
# One key to start scanning
QrCodeAuthenticationClient().startScanning(domId, options)
One-click to scan code
# Parameters
domId
<string> ID of the DOM element.options
<Object>options.interval
<number> Interval time, in milliseconds, the default is 800 millisecondsoptions.onStart
<Function> The event callback function to start polling, the first parameter is the timer returned by setInterval, you can use clearInterval to cancel this timeroptions.onResult
<Function> Get the latest status event callback function of the QR code. The type of the first parameter is QRCodeStatus.options.onScanned
<Function> The callback function for the user's first scan code event. At this time, the user has not authorized yet, and the callback user information only contains the nickname and avatar for display purposes. For security reasons, by default, userInfo will only contain two fields, nickname and photo. Developers can also configure in the background to return complete user information.options.onSuccess
<Function> The user agrees to the authorization event callback function. This function will only call back once, after which the polling ends. The first parameter is userInfo user information, and the second parameter is ticket, which is used in exchange for user details. See https://docs.authing.co/scan-qrcode/app-qrcode/customize.html for details. The ticket can be used to exchange for complete user information, see https://docs.authing.co/scan-qrcode/app-qrcode/full-api-list.html for related interfaces.options.onCancel
<Function> The callback function of the user cancel authorization event. This event will only be called back once, after which the polling ends.options.onError
<Function> The callback function for the failure to get the QR code status. Common reasons are network failures, etc., and it will call back every time a query fails. Examples of callback parameter data are {"code": 2241,"message": "QR code does not exist"}options.onExpired
<Function> will be called back when the QR code is invalid, only once, after which the polling ends.options.onCodeShow
<Function> The event when the QR code is successfully displayed for the first time.options.onCodeLoaded
<Function> The first successful loading event of the QR code.options.onCodeLoadFailed
<Function> The event of QR code loading failure.options.onCodeDestroyed
<Function> The event when the QR code is destroyed.options.size
<Object> QR code image size, the default is 240 * 240, the unit is px.options.size.height
<number> heightoptions.size.width
<number> widthoptions.containerSize
<Object> DOM container size, the default is 300 * 300, and the unit is px.options.containerSize.height
<number> heightoptions.containerSize.width
<number> widthoptions.tips
<Object> custom tipsoptions.tips.title
<number>options.tips.scanned
<number>options.tips.succeed
<Object>options.tips.canceled
<number>options.tips.expired
<number>options.tips.retry
<number>options.tips.failed
<number>
# Example
authenticationClient.wxqrcode.startScanning("qrcode", {
onSuccess: (userInfo, ticket) => {
console.log(userInfo, ticket)
},
onError: (message) => onFail && onFail(`${message}`),
});
# return value
null
# Generate QR code
QrCodeAuthenticationClient().geneCode()
Generate QR code
# Parameters
# Example
const authenticationClient = new AuthenticationClient({
appId: "YOUR_APP_ID",
})
const {url, random} = await authenticationClient.wxqrcode.geneCode()
# random QR code unique ID
# url QR code link
# return value
Promise<QRCodeGenarateResult>
# Detect scan code status
QrCodeAuthenticationClient().checkStatus(random)
Check scan code status
# Parameters
random
<string>
# Example
const authenticationClient = new AuthenticationClient({
appId: "YOUR_APP_ID",
})
const {random, status, ticket, userInfo} = await authenticationClient.wxqrcode.checkStatus('RANDOM')
# status: QR code status: 0-not used, 1-scanned code, 2-authorized, 3-canceled authorization, -1-expired
# ticket: A random string used to exchange user information
# userInfo: User information
# return value
Promise<QRCodeStatus>
# Use ticket to exchange user information
QrCodeAuthenticationClient().exchangeUserInfo(ticket)
Use ticket to exchange user information
# Parameters
ticket
<string> ticket
# Example
const authenticationClient = new AuthenticationClient({
appId: "YOUR_APP_ID",
})
const user = await authenticationClient.wxqrcode.exchangeUserInfo('TICKET')
# user: Complete user information, where user.token is the user's login credentials.
# return value
Promise<Partial<User>>
# Start to poll the QR code status
QrCodeAuthenticationClient().startPolling(random, options)
Start to poll the QR code status
# Parameters
random
<string> QR code unique IDoptions
<Object>options.interval
<number> Interval time, in milliseconds, the default is 800 millisecondsoptions.onStart
<Function> The event callback function to start polling, the first parameter is the timer returned by setInterval, you can use clearInterval to cancel this timeroptions.onResult
<Function> Get the latest status event callback function of the QR code. The type of the first parameter is QRCodeStatus.options.onScanned
<Function> The callback function for the user's first scan code event. At this time, the user has not authorized yet, and the callback user information only contains the nickname and avatar for display purposes. For security reasons, by default, userInfo will only contain two fields, nickname and photo. Developers can also configure in the background to return complete user information.options.onSuccess
<Function> The user agrees to the authorization event callback function. This function will only call back once, after which the polling ends. The first parameter is userInfo user information, and the second parameter is ticket, which is used in exchange for user details. See https://docs.authing.co/scan-qrcode/app-qrcode/customize.html for details. The ticket can be used to exchange for complete user information, see https://docs.authing.co/scan-qrcode/app-qrcode/full-api-list.html for related interfaces.options.onCancel
<Function> The callback function of the user cancel authorization event. This event will only be called back once, after which the polling ends.options.onError
<Function> The callback function for the failure to get the QR code status. Common reasons are network failures, etc., and it will call back every time a query fails. Examples of callback parameter data are {"code": 2241,"message": "QR code does not exist"}options.onExpired
<Function> will be called back when the QR code is invalid, only once, after which the polling ends.
# Example
# return value
null