Code Monkey home page Code Monkey logo

nodejs-sample-app's Introduction

Node.js Sample App

Sample Node.js App integrating SAWO for authentication

Instructions for Getting Started

  • Install express by running
npm install express
  • Make some endpoints in index.js file and serve your html files
const 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."));
  • The body of the 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'
      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 and can take actions according to that
  • Demo body for the success.html file
<body>
  <h2 id="login" style="text-align: center">
    You Have Logged In Successfully !
  </h2>

  <script>
    // Fetching payload from sessionStorage
    const payload = sessionStorage.getItem("payload");
    // Checking if the payload is available or not
    if (payload) {
      // If the payload is available then console.log the payload
      console.log("Payload : " + payload);
    } else {
      // If the payload isn't available, that means the user hasn't logged in yet.
      // So redirecting back to "/login"
      window.location.href = "/login";
    }
  </script>
</body>

Feel free to add your own styling and elements

nodejs-sample-app's People

Contributors

udayprakash1997 avatar kswarn avatar voidniloy avatar kswarna99 avatar niloysikdar avatar

Forkers

tejaputchala

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.