Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    Invalid totp

    Bugs
    5
    7
    29
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hemil.quarec last edited by

      Here is my code

      
      // Initialize SmartAPI
      let smart_api;
      try {
          smart_api = new SmartAPI({
              api_key: process.env.SMART_API_KEY,    // Your API Key
              clientId: process.env.CLIENT_ID,       // Your Client ID
              clientSecret: process.env.CLIENT_SECRET // Your Client Secret
          });
          console.log('SmartAPI initialized successfully');
      } catch (error) {
          console.error('Error initializing SmartAPI:', error);
          process.exit(1);
      }
      
      // Store session data
      let session = null;
      
      // Session check middleware
      const checkSession = (req, res, next) => {
          if (!session || !session.feedToken) {
              return res.status(401).json({ success: false, message: 'Not logged in or session expired' });
          }
          next();
      };
      
      //login code test
      app.post('/api/login', async (req, res) => {
          try {
              console.log('Login attempt started...');
              const { client_code, password, totp } = req.body;
              
              if (!client_code || !password || !totp) {
                  throw new Error('Missing required login parameters');
              }
      
              console.log('Generating session...');
              const data = await smart_api.generateSession(client_code, password, totp);
              
              console.log('Full API Response:', JSON.stringify(data, null, 2));
      
              if (!data) {
                  throw new Error('Session generation failed, missing tokens.');
              }
      
              console.log('Session generated successfully:', {
                  hasAccessToken: !!data.accessToken,
                  hasRefreshToken: !!data.refreshToken,
                  hasJwtToken: !!data.jwtToken,
                  hasFeedToken: !!data.feedToken
              });
      
              session = data;
              smart_api.jwtToken = data.jwtToken;
              smart_api.feedToken = data.feedToken;
      
              res.json({
                  success: true,
                  message: 'Login successful',
                  sessionInfo: {
                      hasAccessToken: !!data.accessToken,
                      hasJwtToken: !!data.jwtToken,
                      hasFeedToken: !!data.feedToken
                  }
              });
      
          } catch (error) {
              console.error('Login error details:', error);
              res.status(500).json({
                  success: false,
                  message: error.message || 'Login failed',
                  errorDetails: error.toString()
              });
          }
      });
      
      
      

      Response

      Generating session...
      Full API Response: {
      "status": false,
      "message": "Invalid totp",
      "errorcode": "AB1050",
      "data": null
      }
      Session generated successfully: {
      hasAccessToken: false,
      hasRefreshToken: false,
      hasJwtToken: false,
      hasFeedToken: false
      }

      ID: "k624646"

      any one help me to solve this.

      P 1 Reply Last reply Reply Quote 0
      • A
        admin last edited by

        @hemil-quarec share your code for TOTP generation.

        1 Reply Last reply Reply Quote 0
        • S
          sabubalakrishnan last edited by

          const otplib = require('otplib');
          // The secret from your otpauth URL
          const secret = 'XXXXXXXXX';
          // Set up the algorithm and other options globally
          otplib.authenticator.options = {
          algorithm: 'sha1', // Use SHA1 algorithm
          digits: 6, // Number of digits in the OTP
          step: 30 // Period in seconds (default is 30 seconds)
          };

          /**

          • Function to generate a TOTP based on a provided secret.
          • @param {string} secret - The shared secret key.
          • @returns {string} - The generated OTP.
            */
            function generateTOTP() {
            return otplib.authenticator.generate(secret);
            }

          module.exports = {
          generateTOTP
          };

          I am using this code to generate TOTP some sometimes getting validate TOTP and sometime not getting it verified

          1 Reply Last reply Reply Quote 0
          • P
            PramodKRao last edited by

            import { totp } from 'otplib';
            const generateTOTP = () => {
              const totpCode = totp.generate("XXXXXXXXXX");
              return totpCode;
            }
            

            this is my code and it always gives me Invalid TOTP

            1 Reply Last reply Reply Quote 0
            • P
              PramodKRao @tester1234 last edited by

              import { totp } from 'otplib';
              const generateTOTP = () => {
                const totpCode = totp.generate("HPM2L5H2NN4REGW36VDUK6BTIU");
                return totpCode;
              }
              

              this is my code this always gives me inval totp

              1 Reply Last reply Reply Quote 0
              • P
                PramodKRao @hemil.quarec last edited by

                @hemil-quarec same problem

                A 1 Reply Last reply Reply Quote 0
                • D
                  DiljeetS last edited by

                  Has this Invalid TOTP issue been resolved for you
                  I am getting same error

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post