Import the Coforio class using one of the following:
import Coforio from '@inventives/coforio-sdk';
const Coforio = require('@inventives/coforio.sdk');
Class Constructor
const cio = new Coforio(productId, productKey);
const cio = new Coforio(productId, productKey, redisHost, redisPassword);
Arguments
- productId - product ID provided to you in the Cofor.io Product Configuration Page
- productKey - product key provided to you in the Cofor.io Product Configuration Page
- redistHost - (Optional) redis instance hostname to use authentication cache
- redisPassword - (Optional) redist instance password to use authentication cache
Returns
- An instantiated Coforio object
Class Methods
cio.Authorize
const login = await cio.Authorize(AUTH_TOKEN, 'member', [], []);
Arguments
- AUTH_TOKEN - authentication token provided upon user login
- access_level - (Optional) required access level, one of:
member(default),admin, orowner - team_scopes - (Optional) list of scopes to be present on team for successful authorization
- member_scopes - (Optional) list of scopes to be present on member for successful authorization
Returns
- Login object with properties,
user,membership, andteam- where team is an object of The "Team" Class.
cio.AuthorizeMiddleware - ExpressJS Authorization Middleware
const middleware = cio.AuthorizeMiddleware('member', [], []);
Arguments
- access_level - (Optional) required access level, one of:
member(default),admin, orowner - team_scopes - (Optional) list of scopes to be present on team for successful authorization
- member_scopes - (Optional) list of scopes to be present on member for successful authorization
Returns
- An authorization middleware function which accepts
req,res, andnextfor use in ExpressJS APIs. See User Authorization for usage.
cio.NewTeam
const team = await cio.NewTeam(token, team_name, [], {}, [], 0);
Arguments
- token - team creation token provided by the Cofor.io team creation OAuth flow
- team_name - name of the new team to create
- scopes - list of scopes the team should have
- data - customer data object to be saved on the team
- subscribe_to_plans - (Optional) a list of Stripe pricing plan ids and quantities to subscribe the team to. See Product Pricing Plans for usage.
- trial_days - (Optional) number of days to provide as free trial for team.
Returns
- An instantiated team object. See The "Team" Class reference.
cio.GetTeamById
const team = await cio.GetTeamById(id);
Arguments
- id - team ID
Returns
- An instantiated team object. See The "Team" Class reference.
cio.QueryTeams
const teams = await cio.QueryTeams({ data: '...'});
Be careful with queries!
You must sanitize user input to
cio.QueryTeamsandcio.QueryMembershipbecause the query is passed directly into a MongoDB query. If an end user is able to input the query, they may gain access to an entire collection!
Use this to query the Teams attached to your product. Accepts MongoDB operators (such as $and or $or) and can be used to get Teams based on their product-specific data property.
Arguments
- query - A mongoose query object
Returns
- An array of Team objects that match the query. See The "Team" Class reference.
cio.ListTeams
const teams = await cio.ListTeams(0, 100);
Arguments
- skip - (Optional) number of teams to skip for pagination
- count - (Optional) number of teams to return for pagination
Returns
- A list of instantiated team objects. See The "Team" Class reference.
cio.setTokenFunc
Set the default function to use to get authorization token from the provided HTTP request object req in use with ExpressJS.
const cio.setTokenFunc(func);
Arguments
- func - the function to retrieve authorization token from the
reqobject. The function must acceptreqand response the token string.
cio.ListMemberships
const memberships = await cio.ListMemberships();
Returns
- A list of team membership objects - with each containing the membership
id,access_level, and the corresponding team member'suserobject.
cio.GetMembershipById, cio.GetMembershipByUserId, cio.getMembershipByUserEmail
const membership = await cio.GetMembershipById(id);
const membership = await cio.GetMembershipByUserId(userId);
const membership = await cio.GetMembershipByUserEmail(userEmail);
Arguments
- id - membership ID
- userId - member user ID
- userEmail - member user Email
Returns
- A membership object containing the membership
id,access_level, and the corresponding team member'suserobject.
cio.QueryMemberships
const memberships = await cio.QueryMemberships({ data: '...' });
Be careful with queries!
You must sanitize user input to
cio.QueryTeamsandcio.QueryMembershipbecause the query is passed directly into a MongoDB query. If an end user is able to input the query, they may gain access to an entire collection!
Use this to query the Memberships attached to your product. Accepts MongoDB operators (such as $and or $or) and can be used to get Memberships based on their product-specific data property.
Arguments
- query - a Mongoose query object
Returns
- An array of all memberships that match the query.
Update a membership
await cio.UpdateMembership(id, { access_level, scopes, data });
Arguments
- id - membership ID
- access_level - (Optional) desired user access level for the team; one of:
member,admin, orowner - scopes - (Optional) a list of access scopes to provide for the membership
- data - (Optional) any custom membership data to be stored for the team member
cio.DeregisterMember
await cio.DeregisterMember(id);
Arguments
- id - membership ID
cio.SignupMember
const { membership, confirm_url } = await cio.SignupMember(token);
Arguments
- token - (Optional) user authorization token to perform actions on behalf of a user
Invite a Member to Product
const invitation = await cio.Invite(email, 'member', [], {}, redirect_uri);
Arguments
- email - new member's email address
- access_level - (Optional) desired user access level for the team; one of:
member,admin, orowner - data - (Optional) any custom membership data to be stored for the team member
- scopes - (Optional) a list of access scopes to provide for the new member
- redirect_uri - (Optional, but HIGHLY RECOMMENDED) URL to send the user to login to on the frontend after account is created.
