Skip to content

Authentication and Users

Every user needs to authenticate with ConnectyCube before using any ConnectyCube functionality.

When someone connects with an application using ConnectyCube, the application will need to obtain a session token which provides temporary secure access to ConnectyCube APIs.

A session token is an opaque string that identifies a user and an application.

Create session token

As a starting point, the user’s session token needs to be created allowing user any further actions within the app. Pass login/email and password to identify a user:

val userToLogin = ConnectycubeUser(login = "marvin18", password = "supersecurepwd")
ConnectyCube.signIn(userToLogin, { user -> }, { error -> })

Note: With the request above, the user is created automatically on the fly upon session creation using the login (or email) and password from the request parameters.

Important: For better security it is recommended to deny the session creation without an existing user.
For this, set ‘Session creation without an existing user entity’ to Deny under the Application -> Overview -> Permissions in the admin panel.

Authentication via phone number

Sign In with phone number is supported with Firebase integration.

The whole guide on how to create Firebase project, connect Firebase SDK and implement authentication via phone number is available at our Firebase Setup Guide. Please follow it.

Authentication via external identity provider

Custom Identity Provider (CIdP) feature is necessary if you have your own user database and want to authenticate users in ConnectyCube against it. It works the same way as Facebook/Twitter SSO.

With Custom Identity Provider feature you can continue use your user database instead of storing/copying user data to ConnectyCube database.

CIdP high level integration flow

To get started with CIdP integration, check the Custom Identity Provider guide which describes high level integration flow.

How to login via CIdP

Once you done with the setup mapping in ConnectyCube Dashboard, it’s time to verify the integration. To perform CIdP login, the same ConnectyCube User Login API is used. You just use existing login request params to pass your external user token:

val userToLogin = ConnectycubeUser(login = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTIzNDU2Nzg5LCJuYW1lIjoiSm9zZXBoIn0.OpOSSw7e485LOP5PrzScxHb7SR6sAOMRckfFwi4rp7o")
ConnectyCube.signIn(userToLogin, { user -> }, { error -> })

Once the login is successful, ConnectyCube will create an underalying User entity, so then you can use ConnectyCube APIs in a same way as you do with a normal login. With CIdP we do not have/store any user password in ConnectyCube User entity.

Following further integration, you may need to connect to Chat. In a case of CIdP login, you do not have a user password. In such cases you should use ConnectyCube session token as a password for chat connection. Follow the Connect to Chat with CIdP guide.

Session expiration

Expiration time for session token is 2 hours after the last request to API is made. But you do not need to worry about it - with the automatic session token management it will be renewed automatically with next request to API.

Destroy session token

To destroy a session use the following code:

ConnectyCube.destroySession({ }, { error -> })

User signup

val userTags = listOf("iphone", "apple")
val userToSignUp = ConnectycubeUser(login = "marvin18", password = "supersecurepwd").apply {
email = "awesomeman@gmail.com"
fullName = "Marvin Simon"
phone = "47802323143"
website = "https://dozensofdreams.com"
tags = userTags.joinToString(",")
}
ConnectyCube.signUp(userToSignUp, { user -> }, { error -> })

Only login (or email) + password are required. Other fields are optional.

User profile update

val userToUpdate = ConnectycubeUser(login = "marvin18", password = "supersecurepwd")
ConnectyCube.updateUser(userToUpdate, { user -> }, { error -> })

If you want to change your password, you need to provide 2 parameters: password and oldPassword. An updated user entity will be returned.

User avatar

You can set a user’s avatar. You just need to upload it to the ConnectyCube cloud storage and then connect to user.

val imageFile: File = ...
ConnectyCube.uploadFile(imageFile.path, successCallback = { cubeFile ->
val jo = JsonObject()
jo.add("avatar_uid", JsonPrimitive(cubeFile.uid))
user.customData = jo.toString()
ConnectyCube.updateUser(user, { user -> }, { error -> })
}, errorCallback = { ex -> })

Now, other users can get you avatar:

val obj = JsonParser().parse(user.customData).asJsonObject
val fileUID = obj.getAsJsonPrimitive("avatar_uid").asString
val avatarUrl = getPrivateUrlForUID(fileUID)

Password reset

It’s possible to reset a password via email:

ConnectyCube.resetPassword("awesomeman@gmail.com", {}, { error -> })

If provided email is valid - an email with password reset instruction will be sent to it.

Retrieve users

Retrieve users by ID

val usersIds = setOf(22, 23)
ConnectyCube.getUsersByIds(usersIds, {usersResult -> }, { error -> })

Retrieve user by login

ConnectyCube.getUserByLogin("amigo", {user -> }, { error -> })

Retrieve user by email

ConnectyCube.getUserByEmail("amigo@gmail.com", {user -> }, { error -> })

Retrieve users by full name

ConnectyCube.getUsersByFullName("Marvin Samuel", {usersResult -> }, { error -> })

Retrieve user by phone number

ConnectyCube.getUserByPhoneNumber("+4427123314", {user -> }, { error -> })

Retrieve user by external ID

ConnectycubeUsers.getUserByExternalId("3789").performAsync(object : EntityCallback<ConnectycubeUser> {
override fun onSuccess(users: ConnectycubeUser, args: Bundle) {
}
override fun onError(error: ResponseException) {
}
})

Retrieve users by tags

val userTags = setOf("iphone")
ConnectyCube.getUsersByTags(userTags, {usersResult -> }, { error -> })

Delete user

A user can delete himself from the platform:

ConnectyCube.deleteUser({}, { error -> })