Authentication and Authorization

Logging in with InstaVision’s Authentication System

To log in with the user account, the following information is typically required:

  • email (required): The email address of the user
  • password (required): The password of the user
InstaVision.userServices.login(
  email = "test@example.com", // The email address of the user
  password = "password", // The password of the user
  onSuccess = { user ->
    // The user object contains the user details
  },
  onError = { apiError ->
    // The error object contains the error code and message
  }
)

Logging in with Third Party Authentication System

To log in with the user account, the following information is typically required:

  • token (required): The jwt token of the user
InstaVision.userServices.login(
  token = "<JWT_TOKEN>", // The jwt token of the user
)

You will also be required to implement the RefreshTokenListener interface to handle token refresh. The onRefreshToken method should return the new token. and will be triggered when ever the requests fail due to token expiration.

class MainActivity : Activity(), RefreshTokenListener {
  override fun onRefreshToken(): String {
    // Implement your logic to refresh the token
    return "<NEW_JWT_TOKEN>"
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    InstaVision.userServices.addRefreshTokenListener(this)
  }
}


Logging in with Google

To log in with a Google account, the following method can be used:

  • idToken (required): The Google ID token obtained from Credential Manager.
InstaVision.userServices.loginWithGoogle(
  idToken = "<GOOGLE_ID_TOKEN>", // The Google ID token
  onSuccess = { user ->
    // The signed in user
  },
  onError = { error ->
    // The error object contains the error code and message
  }
)

Checking if password authentication is available

Returns true if the currently signed-in Firebase user has an email/password provider linked, false if the user signed in only through a social provider — or nobody is signed in. Use it after sign-in, for example to decide whether to show a Change Password screen:

if (InstaVision.userServices.isPasswordAuthEnabled()) {
  // Show the change-password option
} else {
  // Social-only account
}

Checking if the user is logged in

To check if the user is logged in, the following code can be used:

viewModelScope.launch {
  InstaVision.userServices.isLoggedIn().collectLatest { isLoggedIn ->
    if (isLoggedIn == true) {
      // The user is logged in
    } else {
      // The user is not logged in    
    }
  }
}

Fetching the Auth Token

To observe the current auth token used by the SDK, the following code can be used:

viewModelScope.launch {
  InstaVision.userServices.fetchToken().collectLatest { token ->
    // The current auth token
  }
}

Logout

To log out the user, the following code can be used:

  • pushToken (optional): The push token of the device to unregister for push notifications.
InstaVision.userServices.logout(pushToken = "pushToken")

isLoggedIn() emits false immediately. The stored token and preferences are cleared — and fetchToken() emits "" — on a background coroutine after the backend logout request completes; that request is only sent when pushToken is non-empty, and failures are ignored.


This site uses Just the Docs, a documentation theme for Jekyll.