Skip to content
Docs

References / SDKs

NodeJS

This implementation uses the express framework to create Node.js API.

  • Create a node project by running the following command in the command line:

    npm init

    Upon completion, it will add a package.json file in the project folder.

  • Create a server file in the project and name it server.js.

  • Add the following code snippet to the server.js file.

code
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
var PORT = 3000;
app.use(bodyParser.urlencoded({
extended: true
}));
// your MojoAuth API implementation goes here
app.listen(PORT, () => console.log('App can be accessed at localhost:' + PORT ));

Note: Make sure you add further code (given in steps below) in place of // your MojoAuth API implementation goes here text mentioned in the above file.

Add project dependency and MojoAuth SDK using npm by running the following command in the command line:

code
npm install express body-parser mojoauth-sdk

Upon installation, you will find MojoAuth Node.js SDK under the node module.

javascript
var config = {
apiKey: "Your API Key",
};
var ma = require("mojoauth-sdk")(config);

Add the calling of the SigninWithMagicLink() function and pass the Email and optional query parameters to send the magiclink.

javascript
let email = "Your Email";
let query = {};
query.language = "Your language";
query.redirect_url = "Your redirect URL";
ma.mojoAPI
.signinWithMagicLink(email, query)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Choose your language and localize your website according to a country or region, checkout Localization guide for more details.

Redirect URL is used to redirect users to your custom url after successful authentication. Checkout Whitelist your domain for successful redirection to your domain URL.

Add the calling of the PingStatus() function and pass the state id received from the SigninWithMagicLink() function.

javascript
let state_id = "Your StateID";
ma.mojoAPI
.pingStatus(state_id)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the ResendMagicLink() function and pass the state id received from the SigninWithMagicLink() function.

javascript
let state_id = "Your State ID";
let query = {};
query.language = "Your language";
query.redirect_url = "Your redirect URL";
ma.mojoAPI
.resendMagicLink(state_id, query)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the SigninWithEmailOTP() function and pass the Email and optional query parameters to send the OTP.

javascript
let email = "Your Email";
let query = {};
query.language = "Your language";
ma.mojoAPI
.signinWithEmailOTP(email, query)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the VerifyEmailOTP() function and pass the OTP and state id received from the SigninWithEmailOTP() function.

javascript
let state_id = "Your StateID";
let otp = "Your OTP";
ma.mojoAPI
.verifyEmailOTP(otp, state_id)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the ResendEmailOTP() function and pass the state id received from the SigninWithEmailOTP() function.

javascript
let state_id = "Your State ID";
let query = {};
query.language = "Your language";
ma.mojoAPI
.resendEmailOTP(state_id, query)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the SigninWithPhoneOTP() function and pass the phone number and optional query parameters to send the OTP.

javascript
let phone = "Your Phone"; // pass the phone number in the international format e.g for US, +1XXXXXXXXXX
let query = {};
query.language = "Your language";
ma.mojoAPI
.signinWithPhoneOTP(phone, query)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the VerifyPhoneOTP() function and pass the OTP and state id received from the SigninWithPhoneOTP() function.

javascript
let state_id = "Your StateID";
let otp = "Your OTP";
ma.mojoAPI
.verifyPhoneOTP(otp, state_id)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the ResendPhoneOTP() function and pass the state id received from the SigninWithPhoneOTP() function.

javascript
let state_id = "Your State ID";
let query = {};
query.language = "Your language";
ma.mojoAPI
.resendPhoneOTP(state_id, query)
.then((response) => {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Add the calling of the verifyToken() function and pass the MojoAuth JWT Token to verify the token.

javascript
var jwtToken = Your JWT Token";
ma.mojoAPI.verifyToken(jwtToken).then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error)
});