Node JS
Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.
Node [Version 12.x+ ] , Node Package Manager (NPM) [Version 6.x+]
1. Install express by running
npm install express
2. Make endpoints in
index.js
file and serve your HTML filesconst express = require("express");
const app = express();
const path = require("path");
app.use(express.static(path.join(__dirname, "public")));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/login", (req, res) => {
res.sendFile(__dirname + "/public/login.html");
});
app.get("/success", (req, res) => {
res.sendFile(__dirname + "/public/success.html");
});
app.listen("3000", console.log("Listening on port 3000."));
3. To use SAWO Login you would need an API key which can be obtained by creating a project in the sawo dashboard.
4. Once you create your project, you would need to set your project name and hostname.
4.1. For development in a local machine, the hostname should be set to 'localhost'.
If using ''localhost" as hostname is not working for you, try "127.0.0.1"
🤓
4.2. For production, the hostname should be set to your domain.
If you are adding your domain do not add 'https://', ''http://', 'www' or even trailing backslash.
Example:
https://dev.sawolabs.com/
should be kept as dev.sawolabs.com
5. Copy the API key from the project and keep it safe and secure.
The best practice to store your API key is to store values in .env so that they are not exposed.
6. The body of the HTML login page should look like this for showing the SAWO login form:
<body>
<div id="sawo-container" style="height: 300px; width: 300px"></div>
<script src="https://websdk.sawolabs.com/sawo.min.js"></script>
<script>
// Fetching payload from sessionStorage
const payload = sessionStorage.getItem("payload");
if (payload) {
// If the payload is available, that means the user has logged in already.
// So redirecting back to "/login"
window.location.href = "/success";
}
var config = {
// should be same as the id of the container created on 3rd step
containerID: "sawo-container",
// can be one of 'email' or 'phone_number_sms' or 'both_email_phone'
identifierType: "email",
// Add the API key copied from 2nd step
apiKey: "Your API Key",
// Add a callback here to handle the payload sent by sdk
onSuccess: (payload) => {
// Storing the payload in sessionStorage
sessionStorage.setItem("payload", JSON.stringify(payload));
// Redirecting to "/success"
window.location.href = "/success";
},
};
var sawo = new Sawo(config);
sawo.showForm();
</script>
</body>
You can store the payload to the sessionStorage, database, or integrate CRM and can take actions according to that.
7. Once the SAWO SDK is successfully set up, a login form will be rendered in the provided container as displayed in the picture below:

Last modified 9mo ago