Socketize will be shutting down soon. We are not accepting new registrations. Sorry about this.

Users and Authentication

Except reading data from public scope, subscribing to channels, login and register, evey action in Socketize needs an authenticated user. Though if you wish you can kind of bypass it by making a default user always logged in, will be discussed later) in this page.

Login or Authenticate

After a user is successfully logged in, the browser will remember the user, they don't have to login on each page load. Browser will forget them after 2 hours of inactivity.

There are basically two methods of user authentication.

Password Based Authentication

Login a user with email/username and password. Simply call:

socketize.login({
    email: 'user_provided_email',
    password: 'user_provided_password'
}).then(function(response) {
    // Successfully logged in
}).catch(function(reponse) {
    // Could not login
});

Payload Based Authentication

It's a way logging user in silently in the background. For example, when you have a project with your custom backend and you use Socketize as part of the project to make use of WebSocket or so, then you don't need user registration/login feature here because in your project you already have this. You just need to login/register user in the background without even letting user know. This process is called Single Sign-On (SSO).

You can create a payload message in your backend using the Socketize private key and email/username of your app's logged in user. After verifying the payload Socketize will silently log the user in. It'll automatically register the user if the user is not in Socketize.

First you've to create a SHA256 HMAC Payload in your backend. In PHP you do it like this:

$payload = hash_hmac('sha256', $loggedInUsersEmailOrUsername, 'your_socketize_apps_private_key');

Pass this payload on client side and call:

socketize.auth({
    email: '<?php echo $userEmail ?>',
    payload: '<?php echo $payload ?>'
}).then(function(response) {
    // Successfully logged in
}).catch(function(reponse) {
    // Could not login
});

If you've created payload using username then use username: '<?php echo $username ?>', instead of email:....

You can also manually generate a payload from your Socketize Dashboard.

Register a User

socketize.register({
    email: 'user_provided_email',
    username: 'user_provided_username',
    password: 'user_provided_password',
}).then(function(response) {
    // Successfully registered
}).catch(function(reponse) {
    // Could not register
});

Log in a User on Instantiation (page load)

To instantly login a user while initialization:

var params = {
    'public_key': 'your_public_key',
    'auth_params': {
        'username': 'your_username', //or email
        'payload': 'your_payload' //or password
    }
};
var socketize = new Socketize.client(params);

Login/Register Popup

Socketize.js has handy popups/modals for login and register. You don't have to code these yourself, but surely you can if you need.

// Will show a login popup
socketize.showLoginModal();

// Will show a register popup
socketize.showRegisterModal();

These forms use Twitter Bootstrap markup so it will adapt to your own CSS style if you're using Bootstrap, or you can override it in your CSS. A default design is already there.

Login/Register Event

You can register callbacks when a user is logged in, registered or logged out. Callbacks are triggered no matter if client uses popup, login(), auth() or register.

socketize.on('login', function(user) {
    $('#login-btn').hide();
    $('header').append('<span>Hi ' + user.username + '</span>');
});

socketize.on('register', function(user) {
    alert('Registration successful, please login!');
});

socketize.on('logout', function() {
    $('#login-btn').show();
});

Logout

socketize.logout();

Check if Logged in

if (socketize.isLoggedIn()) {

}

Get Logged in User Data

// Will be null if not logged in
var user = socketize.getUser();
console.log(user.id, user.email, user.username, user.created_at);

Bypass Authentication Feature

If you want to bypass authentication feature let visitor fully access Socketize database you have to do a little trick.

Once you have a Socketize application you get default user with username admin. Make a payload for the username admin by going here.

Copy the payload and use it like this when you create your socketize instance:

var params = {
    'public_key': 'your_public_key',
    'auth_params': {
        'username': 'admin',
        'payload': 'generated_payload_for_admin'
    }
};
var socketize = new Socketize.client(params);

Note: admin user has all the permissions, you can use any user here with less privileges.