SIWW
  • Sign-in with Wallet (SIWW)
  • Specifications
    • Abstract
    • Motivation
    • Open source libraries
    • Security considerations
  • Use Cases
    • NodeJS - Backend implementation
      • 1. Setup passport-wallet
      • 2. Connect to SIWW
      • 3. Manage your authentication token
      • 4. Add authentication routes
      • 5. Code the views
      • 6. Full sample app
      • 7. Debugging and Q&A
    • ReactJS - connect to backend
      • 1. Add a login route
      • 2. Redirect the route to your backend
    • JS - Native calls to SIWW libs
      • 1. Initialize SIWW client lib
      • 2. Connect to a Wallet
      • 3. Create a message
      • 4. Send a message
      • 5. React JS App
    • Authenticating as an end-user
      • 1. Have a wallet available
      • 2. Connecting and signing
Powered by GitBook
On this page
  1. Use Cases
  2. NodeJS - Backend implementation

3. Manage your authentication token

We want to manage our own cookie for authenticating the user into our app. We will user another npm library for this.

npm install jsonwebtoken

We create a new file "authenticate/token.js" in which we will manage our own authentication token.

const jsonwebtoken = require("jsonwebtoken");

module.exports = {
    getCookieName,
    getCookieOptions,
    createToken
};

    // name our cookie
    function getCookieName() {
        return "my_jwt_token";
    }

    // same site cookie info...
    function getCookieOptions() {
        var objOptions= {
            sameSite: "Lax"
        };
        if(!gConfig.isDebug){
            objOptions.secure=true;
        }
        return objOptions
    }

    // JWT signed payload 
    function createToken(req) {
        let payload = {
            username: (req!=null && req.user!=null && req.user.username!=null) ? req.user.username.toString() : null,
            connector: (req!=null && req.user!=null && req.user.connector!=null) ? req.user.connector : null,
            blockchain: (req!=null && req.user!=null && req.user.blockchain!=null) ? req.user.blockchain : null,
            authorizations: (req!=null && req.user!=null && req.user.authorizations!=null) ? req.user.authorizations : null,
            provider_wallet: (req!=null && req.user!=null && req.user.provider_wallet!=null) ? req.user.provider_wallet : null,
            wallet_address: (req!=null && req.user!=null && req.user.wallet_address!=null) ? req.user.wallet_address : null
        };
        return jsonwebtoken.sign(payload, "mySecret", {
            expiresIn: "72h"
        });
    }
Previous2. Connect to SIWWNext4. Add authentication routes

Last updated 2 years ago