Skip to content

Device Authentication

When you connect to an Tap device, your are an Anonymous user. You can access some public information depending on the device configuration.

To access more resources, you may have to login with a different profile.

Login

Start a authenticated session.

import { Tap } from '@iotize/device-client.js/device';

let tap: Tap; // ...

let result: Promise<boolean> = tap.login('username', 'password');

result
  .then(() => {
    // Login successful
  })
  .catch((err) => {
    // An error occurred
    // Maybe device did not respond / Invalid credentials
  });

import com.iotize.android.device.device.api.AuthStateChangeCallback;

// ...

IoTizeDevice device = ...;

device
    .login("myusername", "mypassword")
    .execute(new AuthStateChangeCallback() {
        @Override
        public void onNewAuthState(DeviceAuth.AuthState authState) {
            Log.d(TAG, "Now logged in with username " + authState.username);
        }

        @Override
        public void onAuthFailure(Throwable error) {
            Log.e(TAG, "Auth error " + error.getMessage(), error);
        }
    });
let tap: TapDevice = ...

try tap.login(username: "bob", password: "****")

To get info about the current session see Get current auth state

Tap will automatically logout the user if there is no more communication for a amount of time (called session lifetime).

Tip

The session life time can be configured with IoTize Studio

Logout

let result: Promise<boolean> = device.logout();

result
  .then(() => {
    // Logout successful
  })
  .catch((err) => {
    // An error occurred
    // Maybe device did not respond ?
  });
import com.iotize.android.device.device.api.AuthStateChangeCallback;

// ...

IoTizeDevice device = ...;

device
    .logout()
    .execute(new AuthStateChangeCallback() {
        @Override
        public void onNewAuthState(DeviceAuth.AuthState authState) {
            Log.d(TAG, "Now log out");
        }

        @Override
        public void onAuthFailure(Throwable error) {
            Log.e(TAG, "Auth error " + error.getMessage(), error);
        }
    });
let tap: TapDevice = ...

try tap.logout()

Change user password

Change password for the current user

let result: Promise<any> = await device.changePassword('mynewpassword');
IoTizeDevice device = ...;
device.changePassword("mynewpassword")

Get current auth state

Auth session state will give you:

  • Current username and it's corresponding identifier
  • Session start time in milliseconds
  • The session lifetime

This informations are read from the tap thanks to the GroupService

import { SessionState, Tap } from '@iotize/device-client.js/device';

let tap: Tap; // init your tap

// Subscribe to session state change
tap.sessionState
  // This gives you an rxjs Observable
  .subscribe((authState: SessionState) => {
    console.log(
      `Current user is now ${authState.name} (id=${authState.groupId})`
    );
    console.log(`Session started at ${authState.startTime}`);
    console.log(`Session lifetime is ${authState.lifeTime} milliseconds`);
  });

// Or simply get the current session state (Since version 0.0.1-alpha.65)
let sessionState = await tap.refreshSessionState();