Navigation

    APPDRAG Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular

    REMINDER

    Please be respectful of all AppDragers! Keep it really civil so that we can make the AppDrag community of builders as embracing, positive and inspiring as possible.

    Protect API from calls outside of the private area

    General Discussion
    4
    5
    282
    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.
    • Dick Honing
      Dick Honing last edited by

      Hi there,

      how can I make sure that an API can only get called after the user has succesfully logged into the private area.

      I know how to protect the html pages, but I'm not yet sure what the best approach is for the API functions.

      Thanks in advance!

      1 Reply Last reply Reply Quote 0
      • Wassim
        Wassim last edited by

        Hi,

        What do you call the private area?

        1 Reply Last reply Reply Quote 0
        • E
          ec Organizer last edited by

          An area/web pages the user has access to after loggin in.

          The private pages then all have the following additional bit of Javascript to protect these pages from direct access without having logged in first:

          <script>
              //Wait for jquery to load before executing code
              addEventListener('JqueryLoaded', function(e) {
                  var readEmail = sessionStorage.getItem('email');
                  var readToken = sessionStorage.getItem('token');
                  if (readEmail == "" || readEmail == null || readToken == "" || readToken == null) {
                      window.location.href = 'login.html';
                  }
              }, false);
          </script>
          

          Now I also want to protect the API functions from being accessed by anybody what has not successfully logged into the private area ...

          1 Reply Last reply Reply Quote 0
          • Daniel Mulroy
            Daniel Mulroy last edited by

            Hi @Dick-Honing and @ec-Organizer,

            It looks like you already have a token system for authentication.

            A solution is to send that token along with every API call from the front-end.

            Then, in your API functions, you 'check' the token before running the actual function/returning any data.

            Something in Node.js might look like:

            if (tokenIsValid(event.POST.token) === false) {
                callback(null, {status: "NOT AUTH"}); // handle this error on the client side
                return // stop code execution
            }
            
            // Then continue with your normal function code here
            
            

            tokenIsValid, in this example, would check the token against the user database to ensure the token was valid.

            1 Reply Last reply Reply Quote 1
            • E
              ec Organizer last edited by

              @Daniel-Mulroy Hi Daniel, thanks! And sorry about the Dick Honing / ecOrganizer confusion. I created a second account to test the sharing settings before handing handing it over to third parties and somehow this account got stuck in Safari ...

              Btw: can I contact you backchannel / via email?

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