QR Code-Based Login Authorization

Last Updated on : 2023-08-17 04:07:41download

The QR code login authorization feature allows your app to scan an authorization QR code and enables multiple devices to log in to the app with the same account. The devices can be central control devices, TV sets, or tablets.

Process

In the following process, the app on the central control device must be included in the allowlist. Otherwise, an error message will be returned. For example, if the Smart Life app is used to scan the QR code of the SDK that is created on the Tuya IoT Development Platform and the app is not included in the allowlist, an error message is returned and the session expires.

QR Code-Based Login Authorization

The following table describes the steps in the process. Five API methods are used in this topic.

No. Purpose Description API method
1 Get the token The device requests to get the token for authorization. getQRCodeToken
2 Generate a QR code Use the returned token to generate a QR code in a specific format.
  • Format: tuyaSmart--qrLogin?token=xxxxxxx
  • Example: tuyaSmart--qrLogin?token=AZc72de000-ec00-4000-9e51-b610fc300000
  • Usage: display the generated QR code on the device screen.
    QR Code-Based Login Authorization
3 Get the login status Poll the server to get the authorization status. If the authorization is successful, the user information is returned and users are navigated to the app homepage for subsequent operations. Note: The polling interval time should not be less than 2 seconds per iteration. QRCodeLogin
4 Scan the QR code The app is used to scan and parse the QR code on the device and authorize the device.
5 Authorize login Send the parsed QR code to the cloud to complete the authorization. QRcodeAuth

Get the token

void getQRCodeToken(String countryCode, IGetQRCodeTokenCallback callback);

Parameters

Parameter Description
countryCode The country code, such as 86.
callback The callback.

Example

ThingHomeSdk.getUserInstance().getQRCodeToken("86", new IGetQRCodeTokenCallback() {
    @Override
    public void onSuccess(String token) {

    }

    @Override
    public void onError(String code, String error) {

    }
});

Query login status

void QRCodeLogin(String countryCode, String token, ILoginCallback callback);

Parameters

Parameter Description
countryCode The country code, such as 86.
token The token granted for login.
callback The callback.

Example

ThingHomeSdk.getUserInstance().QRCodeLogin("86", "xxxx", new ILoginCallback() {
    @Override
    public void onSuccess(User user) {
        if (user != null && !TextUtils.isEmpty(user.getSid())){
            ThingHomeSdk.getUserInstance().loginSuccess(user);
            // The value of `homeId` for the target device.
            Object homeId = user.getExtras().get("homeId");

            gotoHomePage();
        }
    }

    @Override
    public void onError(String code, String error) {

    }
});

Authorize login

This method only applies to central control devices. The app is used to scan and parse the QR code on the device and authorize login to the device.

API description

void QRcodeAuth(String countryCode, long homeId, String token, IBooleanCallback callback);

Parameters

Parameter Description
countryCode The country code, such as 86.
homeId The home ID. For more information, see Home Management.
token The token granted for login.
callback The callback.

Example

ThingHomeSdk.getUserInstance().QRcodeAuth("86", mHomeId, getActivityToken(), new IBooleanCallback() {
    @Override
    public void onSuccess() {

    }

    @Override
    public void onError(String code, String error) {

    }
});