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.

Session token rights

There are different types of session tokens to support different use cases:

Session Token TypeDescription
Application session tokenThis kind of access token is needed to read the app data. Has only READ access to resources
User session tokenThe user token is the most commonly used type of token. This kind of access token is needed any time the app calls an API to read, modify or write a specific user’s data on their behalf. Has READ/WRITE access to resources

Session token management

iOS SDK supports automatic session management so you do not need to create a session token on iOS manually:

  • on your 1st request to API the session token will be created automatically
  • when the session expires, any further API request will renew it.

Upgrade session token (user login)

If you have an application session, you can upgrade it to a user session by calling logIn method:

ConnectyCube().signIn(user: userToLogin, successCallback: {(user) in
}) { (error) in
}

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 need to use an external database to authenticate ConnectyCube application users. It allows you to integrate your existing users base with ConnectyCube easily and works the same way as Facebook/Twitter SSO.

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

More info available on a dedicated Custom Identity Provider guide page.

After the Custom Identity Provider configuration, you can use same ConnectyCube user login API to authenticate ConnectyCube users against an external identity provider base.

Downgrade session token (user logout)

If you have a user session, you can downgrade it to an application session by calling logOut method:

ConnectyCube().signOut(successCallback: {
}) { (error) in
}

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 (only v1 for now).

Destroy session token

To destroy a session use the following code:

ConnectyCube().destroySession(successCallback: {
}) { (error) in
}

User signup

let user: ConnectycubeUser = ConnectycubeUser()
user.login = "marvin18"
user.password = "supersecurepwd"
user.email = "awesomeman@gmail.com"
user.fullName = "Marvin Simon"
user.phone = "47802323143"
user.website = "https://dozensofdreams.com"
user.tags = "iphone, apple"
ConnectyCube().signUp(user: user, successCallback: {(user) in
}) { (error) in
}

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

User profile update

let user: ConnectycubeUser = ConnectycubeUser()
user.login = "marvin18"
user.fullName = "Marvin Simon"
ConnectyCube().updateUser(user: user, successCallback: {(user) in
}) { (error) in
}

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 the user.

let imageFilePath = ""
ConnectyCube().uploadFile(filePath: imageFilePath, public: false, successCallback: {(cubeFile) in
let customData = ["avatar_uid" : cubeFile.uid]
if let theJSONData = try? JSONSerialization.data(withJSONObject: customData, options: .prettyPrinted) {
user.customData = String(data: theJSONData, encoding: .utf8)
}
ConnectyCube().updateUser(user: user, successCallback: {(user) in }) { (error) in }
}, errorCallback: { (error) in
}, progress: { (progress) in
})

Now, other users can get your avatar:

let user = ...
if let data = user.customData?.data(using: .utf8) {
if let customData = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : String] {
let avatarUID = customData["avatar_uid"]
let privateAvatarUrl = ConnectycubeFileKt.getPrivateUrlForUID(uid: avatarUID)
}
}

Password reset

It’s possible to reset a password via email:

ConnectyCube().resetPassword(email: "user@mail.com", successCallback: {
}) { (error) in
}

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

Retrieve users

Retrieve users by ID

let paginator = RequestPagination(page: 0, itemsPerPage: 10)
let sorter = RequestSorter(fieldName: "created_at", sortType: OrderType.shared.ASC)
ConnectyCube().getUsersByIds(ids: [21, 22], pagination: paginator, sorter: sorter, successCallback: {(result) in
let users = result.items
}) { (error) in
}

Retrieve user by login

ConnectyCube().getUserByLogin(login: "amigo", successCallback: {(user) in
}) { (error) in
}

Retrieve user by email

ConnectyCube().getUserByEmail(email: "amigo@mail.com", successCallback: {(user) in
}) { (error) in
}

Retrieve users by full name

let paginator = RequestPagination(page: 0, itemsPerPage: 10)
let sorter = RequestSorter(fieldName: "created_at", sortType: OrderType.shared.ASC)
ConnectyCube().getUsersByFullName(fullName: "Marvin", pagination: paginator, sorter: sorter, successCallback: {(result) in
let users = result.items
}) { (error) in
}

Retrieve user by phone number

ConnectyCube().getUserByPhoneNumber(phone: "+4427123314", successCallback: {(user) in
}) { (error) in
}

Retrieve user by external ID

ConnectyCube().getUserByExternalId(id: "3789", successCallback: {(user) in
}) { (error) in
}

Retrieve users by tags

let paginator = RequestPagination(page: 0, itemsPerPage: 10)
let sorter = RequestSorter(fieldName: "created_at", sortType: OrderType.shared.ASC)
ConnectyCube().getUsersByTags(tags: ["iphone"], pagination: paginator, sorter: sorter, successCallback: {(result) in
let users = result.items
}) { (error) in
}

Delete user

A user can delete himself from the platform:

ConnectyCube().deleteUser(successCallback: {
}) { (error) in
}