hasUserToken Property

Summary

Indicates that this document has an associated user token.

Syntax
TypeScript
JavaScript
Object.defineProperty(CacheInfo.prototype, 'hasUserToken', 
   get: function(), 
   set: function(value) 
) 
hasUserToken: boolean; 

Property Value

true if this document has an associated user token; otherwise, false. The default value is false.

Remarks

User tokens can optionally be used with a document to restrict usage. Refer to Loading Documents Using LEADTOOLS Document Library for more information.

Example

This example loads a document from a URL into the cache and associates it with a user token. It will then try to load the document with and without a user token.

UserToken.ts
DocumentHelper.ts
UserToken.js
DocumentHelper.js
UserToken.html
import { DocumentHelper } from "../../utilities/DocumentHelper"; 
 
export class DocumentFactory_UserTokenExample { 
    private _userToken = "my-secret"; 
    private _sendUserToken = false; 
 
    public constructor() { 
        lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
        DocumentHelper.initFactory(); 
    } 
 
    public run = (buttonID: string) => { 
        document.getElementById(buttonID).onclick = this.runCacheExample; 
    } 
 
    runCacheExample = () => { 
        // We will be converting this TIFF file to PDF 
        const imageUrl = "https://demo.leadtools.com/images/tiff/ocr.tif"; 
        const docId = "my-id"; 
        const loadDocumentOptions = new lt.Document.LoadDocumentOptions(); 
        loadDocumentOptions.documentId = docId; 
 
        lt.Document.DocumentFactory.prepareAjax.add(this.prepareAjaxListener); 
        this.addUserTokenSupport(); 
 
        DocumentHelper.log("Loading a document with a user token"); 
        lt.Document.DocumentFactory.loadFromUri(imageUrl, loadDocumentOptions) 
            .done((doc: lt.Document.LEADDocument) => { 
                DocumentHelper.log("Document loaded"); 
 
                // Remove user token support 
                this.removeUserTokenSupport(); 
 
                // Try to load the file without a user-token 
                // This should fail 
                DocumentHelper.log("Loading the document without a user token"); 
                this.loadFromCache(docId, this.shouldntSucceedCallback, () => { 
                    // Set the User Token back and load the document again 
                    this.addUserTokenSupport(); 
                    this.loadFromCache(docId, (doc: lt.Document.LEADDocument) => { 
                        // Make sure the load worked 
                        DocumentHelper.log("Did load fail: " + !doc, doc); 
 
                        // Remove token support and try and delete the doc 
                        this.removeUserTokenSupport(); 
                        lt.Document.DocumentFactory.deleteFromCache(docId) 
                            .done(() => { 
                                DocumentHelper.log("Document shouldn't have been deleted"); 
                            }) 
                            .fail(() => { 
                                DocumentHelper.log("Failed to delete Document"); 
                                // Add token support back, and try to delete again. 
                                this.addUserTokenSupport(); 
                                lt.Document.DocumentFactory.deleteFromCache(docId) 
                                    .done(() => { 
                                        DocumentHelper.log("Document successfully deleted"); 
                                    }) 
                                    .fail(DocumentHelper.showServiceError); 
                            }) 
                    }, DocumentHelper.showServiceError); 
                }); 
            }) 
            .fail(DocumentHelper.showServiceError); 
    } 
 
    saveToCache = (document: lt.Document.LEADDocument, done: () => void) => { 
        lt.Document.DocumentFactory.saveToCache(document) 
            .done(done) 
            .fail(DocumentHelper.showServiceError); 
    } 
 
    loadFromCache = (id: string, callback: (doc: lt.Document.LEADDocument) => void, fail: (jqXHR: any, statusText: any, errorThrown: any) => void) => { 
        lt.Document.DocumentFactory.loadFromCache(id) 
            .done(callback) 
            .fail(fail); 
    } 
 
    addUserTokenSupport = () => { 
        DocumentHelper.log("User token support added"); 
        this._sendUserToken = true; 
    } 
 
    removeUserTokenSupport = () => { 
        DocumentHelper.log("User token support removed"); 
        this._sendUserToken = false; 
    } 
 
    shouldntSucceedCallback = () => { 
        DocumentHelper.log("Should not have succeeded"); 
    } 
 
    prepareAjaxUserToken = (sender: any, e: lt.Document.PrepareAjaxEventArgs) => { 
        // If the headers don't exist, initialize them first 
        if (!e.settings.headers) { 
            e.settings.headers = {}; 
        } 
 
        e.settings.headers["user-token"] = this._userToken; 
    } 
 
    prepareAjaxListener = (sender: any, e: lt.Document.PrepareAjaxEventArgs) => { 
        if (this._sendUserToken) 
            this.prepareAjaxUserToken(sender, e); 
 
        if (e.settings.headers) { 
            DocumentHelper.log("User Token header value: " + e.settings.headers["user-token"]); 
            DocumentHelper.log("Header dump available in console", e.settings.headers); 
            return; 
        } 
 
        DocumentHelper.log("No headers sent."); 
    } 
 
} 
export class DocumentHelper { 
   static showServiceError = (jqXHR, statusText, errorThrown) => { 
      alert("Error returned from service. See the console for details."); 
      const serviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown); 
      console.error(serviceError); 
   } 
 
   static log = (message: string, data?: any) => { 
      const outputElement = document.getElementById("output"); 
      if (outputElement) { 
         const time = (new Date()).toLocaleTimeString(); 
         const textElement = document.createElement("p"); 
         textElement.innerHTML = (outputElement.childElementCount + 1) + " [" + time + "]: " + message; 
         outputElement.insertBefore(textElement, outputElement.firstChild); 
      } 
 
      if (!data) 
         console.log(message); 
      else 
         console.log(message, data); 
   } 
 
   static initFactory = () => { 
      // To communicate with the DocumentsService, it must be running! 
      // Change these parameters to match the path to the service. 
      lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
      lt.Document.DocumentFactory.servicePath = ""; 
      lt.Document.DocumentFactory.serviceApiPath = "api"; 
   } 
} 
import { DocumentHelper } from "../../utilities/DocumentHelper"; 
 
export class DocumentFactory_UserTokenExample { 
    _userToken = "my-secret"; 
    _sendUserToken = false; 
 
    constructor() { 
        lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/js/LEADTOOLSEVAL.txt", "EVAL", null); 
        DocumentHelper.initFactory(); 
    } 
 
    run = (buttonID) => { 
        document.getElementById(buttonID).onclick = this.runCacheExample; 
    } 
 
    runCacheExample = () => { 
        // We will be converting this TIFF file to PDF 
        const imageUrl = "https://demo.leadtools.com/images/tiff/ocr.tif"; 
        const docId = "my-id"; 
        const loadDocumentOptions = new lt.Document.LoadDocumentOptions(); 
        loadDocumentOptions.documentId = docId; 
 
        lt.Document.DocumentFactory.prepareAjax.add(this.prepareAjaxListener); 
        this.addUserTokenSupport(); 
 
        DocumentHelper.log("Loading a document with a user token"); 
        lt.Document.DocumentFactory.loadFromUri(imageUrl, loadDocumentOptions) 
            .done((doc) => { 
                DocumentHelper.log("Document loaded"); 
 
                // Remove user token support 
                this.removeUserTokenSupport(); 
 
                // Try to load the file without a user-token 
                // This should fail 
                DocumentHelper.log("Loading the document without a user token"); 
                this.loadFromCache(docId, this.shouldntSucceedCallback, () => { 
                    // Set the User Token back and load the document again 
                    this.addUserTokenSupport(); 
                    this.loadFromCache(docId, (doc) => { 
                        // Make sure the load worked 
                        DocumentHelper.log("Did load fail: " + !doc, doc); 
 
                        // Remove token support and try and delete the doc 
                        this.removeUserTokenSupport(); 
                        lt.Document.DocumentFactory.deleteFromCache(docId) 
                            .done(() => { 
                                DocumentHelper.log("Document shouldn't have been deleted"); 
                            }) 
                            .fail(() => { 
                                DocumentHelper.log("Failed to delete Document"); 
                                // Add token support back, and try to delete again. 
                                this.addUserTokenSupport(); 
                                lt.Document.DocumentFactory.deleteFromCache(docId) 
                                    .done(() => { 
                                        DocumentHelper.log("Document successfully deleted"); 
                                    }) 
                                    .fail(DocumentHelper.showServiceError); 
                            }) 
                    }, DocumentHelper.showServiceError); 
                }); 
            }) 
            .fail(DocumentHelper.showServiceError); 
    } 
 
    saveToCache = (documentt, done) => { 
        lt.Document.DocumentFactory.saveToCache(document) 
            .done(done) 
            .fail(DocumentHelper.showServiceError); 
    } 
 
    loadFromCache = (id, callback) => { 
        lt.Document.DocumentFactory.loadFromCache(id) 
            .done(callback) 
            .fail(fail); 
    } 
 
    addUserTokenSupport = () => { 
        DocumentHelper.log("User token support added"); 
        this._sendUserToken = true; 
    } 
 
    removeUserTokenSupport = () => { 
        DocumentHelper.log("User token support removed"); 
        this._sendUserToken = false; 
    } 
 
    shouldntSucceedCallback = () => { 
        DocumentHelper.log("Should not have succeeded"); 
    } 
 
    prepareAjaxUserToken = (sender, e) => { 
        // If the headers don't exist, initialize them first 
        if (!e.settings.headers) { 
            e.settings.headers = {}; 
        } 
 
        e.settings.headers["user-token"] = this._userToken; 
    } 
 
    prepareAjaxListener = (sender, e) => { 
        if (this._sendUserToken) 
            this.prepareAjaxUserToken(sender, e); 
 
        if (e.settings.headers) { 
            DocumentHelper.log("User Token header value: " + e.settings.headers["user-token"]); 
            DocumentHelper.log("Header dump available in console", e.settings.headers); 
            return; 
        } 
 
        DocumentHelper.log("No headers sent."); 
    } 
 
} 
export class DocumentHelper { 
   static showServiceError = (jqXHR, statusText, errorThrown) => { 
      alert("Error returned from service. See the console for details."); 
      const serviceError = lt.Document.ServiceError.parseError(jqXHR, statusText, errorThrown); 
      console.error(serviceError); 
   } 
 
   static log = (message, data) => { 
      const outputElement = document.getElementById("output"); 
      if (outputElement) { 
         const time = (new Date()).toLocaleTimeString(); 
         const textElement = document.createElement("p"); 
         textElement.innerHTML = (outputElement.childElementCount + 1) + " [" + time + "]: " + message; 
         outputElement.insertBefore(textElement, outputElement.firstChild); 
      } 
 
      if (!data) 
         console.log(message); 
      else 
         console.log(message, data); 
   } 
 
   static initFactory = () => { 
      // To communicate with the DocumentsService, it must be running! 
      // Change these parameters to match the path to the service. 
      lt.Document.DocumentFactory.serviceHost = "http://localhost:40000"; 
      lt.Document.DocumentFactory.servicePath = ""; 
      lt.Document.DocumentFactory.serviceApiPath = "api"; 
   } 
} 
<!doctype html> 
<html lang="en"> 
<title>Document Example | UserToken</title> 
 
<head> 
   <script src="https://code.jquery.com/jquery-2.2.4.min.js" 
      integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> 
 
   <script src="../../LT/Leadtools.js"></script> 
   <script src="../../LT/Leadtools.Controls.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Engine.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Designers.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Rendering.Javascript.js"></script> 
   <script src="../../LT/Leadtools.Annotations.Automation.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Main.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Color.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Core.js"></script> 
   <script src="../../LT/Leadtools.ImageProcessing.Effects.js"></script> 
   <script src="../../LT/Leadtools.Document.js"></script> 
   <script src="../../LT/Leadtools.Document.Viewer.js"></script> 
   <link rel="stylesheet" type="text/css" href="../../css/examples.css"> 
 
   <!-- All demo files are bundled and appended to the window --> 
   <script src="../../bundle.js" type="text/javascript"></script> 
</head> 
 
<body> 
   <div> 
      <button type="button" id="exampleButton">Run Example</button> 
   </div> 
   <div id="output"></div> 
   <div> 
      <img id="img" /> 
   </div> 
</body> 
 
<script> 
   window.onload = () => { 
      const example = new window.examples.DocumentFactory.UserToken(); 
      example.run("exampleButton"); 
   }; 
</script> 
</html> 
Requirements

Target Platforms

Help Version 22.0.2023.2.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.