undefined

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 Type Description
Application session token This kind of access token is needed to read the app data. Has only READ access to resources
User session token The 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:

[CYBRequest logInWithUserLogin:@"userLogin" password:@"userPassword" successBlock:^(CYBUser *user) {

 } errorBlock:^(NSError *error) {

}];
Request.logIn(withUserLogin: "userLogin", password: "userPassword", successBlock: { (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:

[CYBRequest logOutWithSuccessBlock:^() {

} errorBlock:^(NSError *error) {

}];
Request.logOut(successBlock: {

}) { (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.

Destroy session token

To destroy a session use the following code:

[CYBRequest destroySessionWithSuccessBlock:^() {

} errorBlock:^(NSError *error) {

}];
Request.destroySession(successBlock: {

}) { (error) in

}

User signup

CYBUser *user = [CYBUser user];
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"];

[CYBRequest signUp:user successBlock:^(CYBUser *user) {

} errorBlock:^(NSError *error) {

}];
let user = User()
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"]

Request.signUp(user, successBlock: { (user) in

}) { (error) in

}

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

User profile update

CYBUpdateUserParameters *updateParameters = [CYBUpdateUserParameters new];
updateParameters.login = @"marvin18";
updateParameters.fullName = @"Marvin Simon";

[CYBRequest updateCurrentUser:updateParameters successBlock:^(CYBUser *user) {

} errorBlock:^(NSError *error) {

}];
let updateParameters = UpdateUserParameters()
updateParameters.login = "marvin18"
updateParameters.fullName = "Marvin Simon"

Request.updateCurrentUser(updateParameters, successBlock: { (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.

UIImage *image = ...
NSData *fileData = UIImagePNGRepresentation(image);

 [CYBRequest uploadFileWithData:fileData fileName:@"FileName" contentType:@"image/png" isPublic:false progressBlock:^(float progress) {

    } successBlock:^(CYBBlob *blob) {

        NSDictionary *customData = @{@"avatar_uid" : blob.UID};
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:customData options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonCustomData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        CYBUpdateUserParameters *parameters = [[CYBUpdateUserParameters alloc] init];
        parameters.customData = jsonCustomData;

        [CYBRequest updateCurrentUser:parameters successBlock:^(CYBUser *user) {

        } errorBlock:^(NSError *error) {

        }];
    } errorBlock:^(NSError *error) {

    }];
let image = ...
let data = UIImagePNGRepresentation(image!)
Request.uploadFile(with: data!, fileName: "FileName", contentType: "image/png", isPublic: false, progressBlock: { (progress) in
}, successBlock: { (blob) in

    let parameters = UpdateUserParameters()
    let customData = ["avatar_uid" : blob.uid]
    if let theJSONData = try? JSONSerialization.data(withJSONObject: customData, options: .prettyPrinted) {
        parameters.customData = String(data: theJSONData, encoding: .utf8)
    }

    Request.updateCurrentUser(parameters, successBlock: { (user) in

    }, errorBlock: { (error) in

    })
}) { (error) in

}

Now, other users can get your avatar:

CYBUser *user = ...
NSData *jsonData = [user.customData dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *customData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSString *avatarUDID = customData[@"avatar_uid"];
NSString *privateUrl = [CYBBlob privateUrlForFileUID:avatarUDID];
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 = Blob.privateUrl(forFileUID: avatarUID)
    }
}

Password reset

It's possible to reset a password via email:

[CYBRequest resetUserPasswordWithEmail:@"user@mail.com"  successBlock:^() {

} errorBlock:^(NSError *error) {

}];
Request.resetUserPassword(withEmail: "user@mail.com", successBlock: {

}) { (error) in

}

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

Retrieve users

Retrieve users by ID

CYBPaginator *paginator = [CYBPaginator limit:10 skip:0];
[CYBRequest usersWithIDs:@[@"21",@"22"] paginator:paginator successBlock:^(CYBPaginator *paginator, NSArray *users) {

} errorBlock:^(NSError *error) {

}];
let paginator = Paginator(limit:10, skip: 0)
Request.users(withIDs: ["21","22"], paginator: paginator, successBlock: { (paginator, users) in

}) { (error) in

}

Retrieve user by login

[CYBRequest userWithLogin:@"amigo" successBlock:^(CYBUser *user) {

} errorBlock:^(NSError *error) {

}];
Request.user(withLogin: "amigo", successBlock: { (user) in

}) { (error) in

}

Retrieve user by email

[CYBRequest userWithEmail:@"amigo@mail.com" successBlock:^(CYBUser *user) {

} errorBlock:^(NSError *error) {

}];
Request.user(withEmail: "amigo@mail.com", successBlock: { (user) in

}) { (error) in

}

Retrieve users by full name

CYBPaginator *paginator = [CYBPaginator limit:10 skip:0];
[CYBRequest usersWithFullName:@"Marvin" paginator:paginator successBlock:^(CYBPaginator *paginator, NSArray *users) {

} errorBlock:^(NSError *error) {

}];
let paginator = Paginator(limit:10, skip: 0)
Request.users(withFullName: "Marvin", paginator: paginator, successBlock: { (paginator, users) in

}) { (error) in

}

Retrieve user by phone number

CYBPaginator *paginator = [CYBPaginator limit:10 skip:0];
[CYBRequest usersWithPhoneNumbers:@[@"+4427123314"] paginator:paginator successBlock:^(CYBPaginator *paginator, NSArray *users) {

} errorBlock:^(NSError *error) {

}];
let paginator = Paginator(limit:10, skip: 0)
Request.users(withPhoneNumbers: ["+4427123314"], paginator: paginator, successBlock: { (paginator, users) in

}) { (error) in

}

Retrieve user by external ID

[CYBRequest userWithExternalID:3789 successBlock:^(CYBUser *user) {

} errorBlock:^(NSError *error) {

}];
Request.user(withExternalID: 3789, successBlock: { (user) in

}) { (error) in

}

Retrieve users by tags

CYBPaginator *paginator = [CYBPaginator limit:10 skip:0];
[CYBRequest usersWithTags:@[@"iphone"] paginator:paginator successBlock:^(CYBPaginator *paginator, NSArray *users) {

} errorBlock:^(NSError *error) {

}];
let paginator = Paginator(limit:10, skip: 0)
Request.users(withTags: ["iphone"], paginator: paginator, successBlock: { (paginator, users) in

}) { (error) in

}

Delete user

A user can delete himself from the platform:

[CYBRequest deleteCurrentUserWithSuccessBlock:^() {

} errorBlock:^(NSError *error) {

}];
Request.deleteCurrentUser(successBlock: {

}) { (error) in

}