sys.auth
This package contains some utilities to work with authentication of the API REST.
sendOtpCode(options)
Request an OTP code for a specific email or phone with the permission of the current user.
Parameters
Name | Type | Required | Default | Description |
---|---|---|---|---|
options |
|
yes |
These options allow you to configure the validity of the code and token to be generated and the destination to which it will be sent. Available option is:
|
Returns
boolean
- True if the code was sent successfully. False if something went wrong.
Samples
// request an OTP code
var operationResult = sys.auth.sendOtpCode({email: "email@slingr.io", inactivity: 1, expiration: 24});
if (operationResult) {
sys.logs.info('OTP code was sent successfully');
} else {
sys.logs.error('Error the OTP code was not sent');
}
createToken(idOrEmailUser, code)
Create an api token for the user represented by the given record (Only available for admins). | Exchange a valid OTP code for a token (Available for any user).
Parameters
Name | Type | Required | Default | Description |
---|---|---|---|---|
idOrEmailUser |
|
no |
ID or email of the user with which the token will be created. |
|
code |
|
no |
The OTP code received by email or phone. |
Returns
object
- An api token object for the user requested or for the email or phone number that was used to request the otp code.
Samples
// request an api token.
// Only available for admins or devs users
var tokenForSpecificUser = sys.auth.createToken('user@email.com');
if (tokenForSpecificUser) {
sys.logs.info('The token for the user is: ' + JSON.stringify(tokenForSpecificUser));
} else {
sys.logs.error('Error generating token');
}
// Any user than have a valid OTP code previously generated
var tokenFromOtpExchange = sys.auth.createToken(null, 'codeSent');
if (tokenFromOtpExchange) {
sys.logs.info('The token for the user is: ' + JSON.stringify(tokenFromOtpExchange));
} else {
sys.logs.error('Error generating token');
}