Skip to content

SQL Authentication

Dominique Debergue edited this page Jan 13, 2019 · 14 revisions

Before starting with this section, make sure you've properly set up the server and have a TLS/SSL connection established to the server when connecting clients.

Creating accounts

With the proper server settings in place and an initialized database, clients can create accounts with the signup() function:

gopherClient.signup("bob", "password", null);

This will add an account to the database with the User name "bob", and password "password". The third parameter are the custom account info columns you wish to be set when signing a client up for an account. The third (custom account info columns) parameter must be an object where the keys are the names of existing columns, and the values are the information to store in that column. For example, say we've established two custom account info columns, email and dob:

gopherClient.signup("bob", "password", {email: "[email protected]", dob: "6-18-1992"});

Now, when we want to know if the sign up went well or something went wrong, we can use the events.signup listener:

gopherClient.addEventListener(gopherClient.events.signup, clientSignup);

function clientSignup(success, error){
    if(!success){
        console.log("Error: [ID - "+error.id+"], [Message - '"+error.m+"']");
    }else{
        console.log("Successfully created account!");
    }
}

Deleting Accounts

A client can delete their account while they're logged off with the deleteAccount() function:

gopherClient.deleteAccount("bob", "password", null);

This will delete the account with the name "bob", using the password "password". The last parameter are the custom account info columns you wish to use for something when deleting an account. The last (custom account info columns) parameter must be an object where the keys are the names of existing columns, and the values are optional information to use in your server callbacks. For example:

gopherClient.deleteAccount("bob", "password", {email: "[email protected]"});

Your delete account server callback will now receive a map[string]interface{} that looks like: {email: "[email protected]"}. It will also get the info stored on the database in the email column, so you can, for instance, compare the two emails to check if the client also supplied the correct email address for the account.

Now, when we want to know if the delete went well or something went wrong, we can use the events.accountDelete listener:

gopherClient.addEventListener(gopherClient.events.accountDelete, accountDeleted);

function accountDeleted(success, error){
    if(!success){
        console.log("Error: [ID - "+error.id+"], [Message - '"+error.m+"']");
    }else{
        console.log("Successfully deleted account.");
    }
}

Login

A client cannot login with SQL features enabled on the server unless they have an account, or are logging in as a guest. Once a client has created their account, they can supply their User name and password to login():

gopherClient.login("bob", "password", false, false, null);

This will log the client in under the User name "bob", using the password "password". The last parameter are the custom account info columns you wish to be set when logging a client in. The last (custom account info columns) parameter must be an object where the keys are the names of existing columns, and the values are optional information to use in your server callbacks. For example:

gopherClient.login("bob", "password", false, false, {email: "[email protected]"});

Your login server callback will now receive a map[string]interface{} that looks like: {email: "[email protected]"}. It will also get the info stored on the database in the email column, so you can, for instance, compare the two emails to check if the client also supplied the correct email address for the account.

To check for a successful login and catch any errors, just use the events.login event listener (example here).

Custom login column

If you have a custom account info column set as the login column, you can instead pass the value for that column you wish to find the account for. For example, say we have the custom account info column, email mentioned above. We set the custom login column as the email column, and now clients would enter their email as the login parameter:

gopherClient.login("[email protected]", "password", false, false, null);

The server will look for the account associated with the email "[email protected]" on the email custom account info column, and log the user "bob" in (assuming they supplied the right password).

If you notice there is lacking information, missing features, or bad explanations, please open an issue. All requests are acceptable and will be taken into consideration, so don't be afraid to ask or report something!

Clone this wiki locally