frontend/express/libs/members

Module contaning functions for basic user operations: login, logout, setup, settings

Description:
  • Module contaning functions for basic user operations: login, logout, setup, settings

Source:
Example
var plugins = require('../../plugins/pluginManager.js'); //need for db
var countlyDb = plugins.dbConnection(countlyConfig); //get db connection
var membersUtility = require("./libs/members.js");
membersUtility.db = countlyDB; //setting db before using any function

Members

(static) countlyConfig

Source:
Properties:
Name Type Description
countlyConfig object

countly configuration object

(static) db

Source:
Properties:
Name Type Description
db object

Data base connection. Needs to be set befoe callng any other function.

(static) emptyPermission

Source:
Properties:
Name Type Description
emptyPermission object

empty crud permission

Methods

(static) checkEmail(email, callback)

Description:
  • Function validates if email is not used by any other member.

Source:
Example
membersUtility.checkEmail(email, function(isFree) {
     if(isFree ===true) {
         //email is not taken
     }
});
Parameters:
Name Type Description
email string

mandatory. E-mail to check

callback function

function with one return value. Returns true if email is not used, false if taken.

(static) checkUsername(username, callback)

Description:
  • Function validates if username is not used by any other member.

Source:
Example
membersUtility.checkUsername(username, function(isFree) {
     if(isFree ===true) {
         //username is not taken
     }
});
Parameters:
Name Type Description
username string

mandatory. Username to check.

callback function

function with one return value. Returns true if username is free, false if taken.

(static) clearReqAndRes(req, res)

Description:
  • Clears all inforamtion about user from session parameters. Used when logging ut user.

Source:
Parameters:
Name Type Description
req object

request object

res object

response object

(static) extendSession(req)

Description:
  • Function to extend user session. Changes time when session expires and also extends token(if passed).

Source:
Parameters:
Name Type Description
req object

request object

Properties
Name Type Description
session.auth_token string

auth token

(static) findByUsernameOrEmail(input, callback)

Description:
  • Searches for a user with the given username/email. Useful for the input from the login prompt.

Source:
Parameters:
Name Type Description
input string

username or the email address of the user we are looking for

callback function

function with one parameter, the member object if a user is found, undefined otherwise

(static) findMembers(query) → {Array.<Object>}

Description:
  • Find Members

Source:
Parameters:
Name Type Description
query Object

query

Returns:

list of members

Type
Array.<Object>

(static) forgot(req, callback)

Description:
  • Sends user password reseting information to given e-mail.

Source:
Example
membersUtility.forgot(req, function(member) {
     if(member) {
        //member found
     }
     else {
        //e-mail not passed or user with this e-mail not found
     }
});
Parameters:
Name Type Description
req object

request object.

Properties
Name Type Description
body.email string

mandatory. User email.

body.lang string

optional. Language.(default "en" - english)

callback function

function with one return value. Returns member object if successful.

(static) login(req, res, callback)

Description:
  • Tries to log in user based passed userame and password. Calls "plugins" methods to notify successful and unsucessful logging in attempts. If successful, sets all session variables and auth token. Passes the member object to the callback if retrieved succesfully, but not necessarily logged in succesfully i.e. a member object will still be returned even if the member was locked. Also passes a boolean parameter to the callback indicating if the login was succesful.

Source:
Example
membersUtility.login(req, res, function(member) {
        if(member) {
            // logged in
        }
        else {
            // failed
        }
    });
Parameters:
Name Type Description
req object

request object

Properties
Name Type Description
body.username string

username

body.password string

password

res object

response object

callback function

callback function. First parameter in callback function is member object, if it could be retrieved succesfully. Second parameter is a boolean that is true when logged in succesfully.

(static) loginWithExternalAuthentication(req, res, callback)

Description:
  • Tries to log in user without verification for external authentication. Similar behavior as the membersUtility.login just bypass the verification as the user is already authenticated by external authentication mechanism such as Active Directory, Azure AD or Ldap

Source:
Example
membersUtility.loginWithExternalAuthentication(req, res, function(member) {
        if(member) {
            // logged in
        }
        else {
            // failed
        }
    });
Parameters:
Name Type Description
req object

request object

Properties
Name Type Description
body.username string

username

res object

response object

callback function

callback function. First parameter in callback function is member object, if it could be retrieved succesfully. Second parameter is a boolean that is true when logged in succesfully.

(static) loginWithToken(req, callback)

Description:
  • Logins user with token

Source:
Parameters:
Name Type Description
req object

request object

callback function

callback function

(static) logout(req, res)

Description:
  • Logs out user - clears session info for request and response object

Source:
Parameters:
Name Type Description
req object

request object

res object

response object

(static) removeMembers(query) → {Array.<Object>}

Description:
  • Remove Members

Source:
Parameters:
Name Type Description
query Object

query

Returns:

list of members

Type
Array.<Object>

(static) reset(req, callback)

Description:
  • Resets user password

Source:
Parameters:
Name Type Description
req object

request object

Properties
Name Type Description
body.password string

mandatory. new password.

body.again string

mandatory.

body.prid string

mandatory. Password reset id.

callback function

function with one two return values. First one is password validation error(false if no error) and second one is member object if reset is sucessful.

(static) settings(req, callback)

Description:
  • Saves changed user settings

Source:
Parameters:
Name Type Description
req object

request object

Properties
Name Type Description
body.username string

mandatory - username (current or new one to chacge to)

body.api_key string

mandatory. User API KEY (current or the one to change to)

body.old_pwd string

Old password. Optional. Passed if changing password.

body.new_pwd string

New password. Optional. Passed if changing password.

callback function

function with two return values. First one is true - if successful (false if not sucessful) and the second one - error message(in some cases).

(static) setup(req, callback)

Description:
  • Sets up first user in Countly(if there is none). Req object is used to get mandatory variables from req.body and also there are variables set to have logged in session for new user.

Source:
Example
membersUtility.setup(req, res, countlyConfig, function(error) {
     if(error) {
         //there is error while setting up user
         // error === "Wrong request parameters" - not all mandatory parameters passed or there was error during creating user
        // error === "User exists" - There is already at least one user.
          // error === "....." - mongo error while getting user count.
     }
     else {
         //Success
     }
 });
Parameters:
Name Type Description
req object

request object

Properties
Name Type Description
body.full_name string

Full name. Mandatory.

body.username string

Username. Mandatory.

body.password string

Password. Mandatory.

body.email string

E-mail. Mandatory.

callback function

Function with one return value - error (if there is one)

(static) updateMember(query, data, upsert) → {Object}

Description:
  • Update Member

Source:
Parameters:
Name Type Description
query Object

query

data Object

data to update

upsert boolean

upsert

Returns:

list of members

Type
Object

(static) verifyCredentials(username, password, callback)

Description:
  • Verifies a user's credentials without logging in.

Source:
Example
membersUtility.verifyCredentials(username, password, function(member) {
        if (member) {
            // logged in
        }
        else {
            // failed
        }
    });
Parameters:
Name Type Description
username string

username or the email address of the user

password string

password

callback function

callback function. First parameter in callback function is member object if logging in is successful.

(inner) argon2Hash(str) → {promise}

Description:
  • Create argon2 hash string

Source:
Parameters:
Name Type Description
str string

string to hash

Returns:

hash promise

Type
promise

(inner) createMember(data, provider, deleteDuplicate) → {Promise.<any>}

Description:
  • Create User for external authentication provider

Source:
Parameters:
Name Type Description
data Object

user data

provider string

auth provider

deleteDuplicate boolean

delete duplicate

Returns:

created or updated user data

Type
Promise.<any>

(inner) getSessionTimeoutInMs(req) → {integer}

Description:
  • Function gets session timeout in ms.

Source:
Parameters:
Name Type Description
req object

requets object

Returns:

Session timeout in ms.

Type
integer

(inner) isArgon2Hash(hashedStr) → {boolean}

Description:
  • Is hashed string argon2?

Source:
Parameters:
Name Type Description
hashedStr string

| argon2 hashed string

Returns:

return true if string hashed by argon2

Type
boolean

(inner) killOtherSessionsForUser(userId, my_token, my_session, countlyDb)

Description:
  • Removes all other active sessions for user

Source:
Parameters:
Name Type Description
userId string

id of the user for which to remove sessions

my_token string

current auth token

my_session string

current session id

countlyDb object

data base reference

(inner) mergePermissions(current, addition)

Description:
  • Merge permission objects

Source:
Parameters:
Name Type Description
current object

current permission object

addition object

permission object to merge

(inner) setLoggedInVariables(req, member, countlyDb, callback)

Description:
  • Sets variables for logged in session

Source:
Parameters:
Name Type Description
req object

request object

member object

member object

countlyDb object

data base reference

callback function

callback function, called after token and variables are set. Returns nothing.

(inner) sha1Hash(str, addSalt) → {string}

Description:
  • Create sha1 hash string

Source:
Parameters:
Name Type Description
str string

string to hash

addSalt boolean

should salt be added

Returns:

hashed string

Type
string

(inner) sha512Hash(str, addSalt) → {string}

Description:
  • Create sha512 hash string

Source:
Parameters:
Name Type Description
str string

string to hash

addSalt boolean

should salt be added

Returns:

hashed string

Type
string

(inner) updateUserPasswordToArgon2(id, password, countlyDb)

Description:
  • Update user password to new sha512 hash

Source:
Parameters:
Name Type Description
id string

id of the user document

password string

password to hash

countlyDb object

data base object

(inner) validatePassword(password) → {vary}

Description:
  • Validate password based on configured settings

Source:
Parameters:
Name Type Description
password string

password to validatePassword

Returns:

returns string if there is error, or false if everything is ok

Type
vary

(inner) verifyArgon2Hash(hashedStr, str) → {promise}

Description:
  • Verify argon2 hash string

Source:
Parameters:
Name Type Description
hashedStr string

argon2 hashed string

str string

string for verify

Returns:

verify promise

Type
promise

(inner) verifyMemberArgon2Hash(username, password, countlyDb, callback)

Description:
  • Verify member for Argon2 Hash

Source:
Parameters:
Name Type Description
username string

| User name

password password

| Password string

countlyDb object

data base object

callback function

| Callback function