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.

TypeScript
JavaScript
function HasUserToken() { 
   const _userToken = "my-secret"; 
   const _sendUserToken = false; 
 
   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(); 
 
   log("Loading a document with a user token"); 
   lt.Document.DocumentFactory.loadFromUri(imageUrl, loadDocumentOptions) 
      .done((doc) => { 
         log("Document loaded"); 
 
         // Remove user token support 
         removeUserTokenSupport(); 
 
         // Try to load the file without a user-token 
         // This should fail 
         log("Loading the document without a user token"); 
         loadFromCache(docId, this.shouldntSucceedCallback, () => { 
            // Set the User Token back and load the document again 
            addUserTokenSupport(); 
            loadFromCache(docId, (doc) => { 
               // Make sure the load worked 
               log("Did load fail: " + !doc, doc); 
 
               // Remove token support and try and delete the doc 
               removeUserTokenSupport(); 
               lt.Document.DocumentFactory.deleteFromCache(docId) 
                  .done(() => { 
                     log("Document shouldn't have been deleted"); 
                  }) 
                  .fail(() => { 
                     log("Failed to delete Document"); 
                     // Add token support back, and try to delete again. 
                     addUserTokenSupport(); 
                     lt.Document.DocumentFactory.deleteFromCache(docId) 
                        .done(() => { 
                           log("Document successfully deleted"); 
                        }) 
                        .fail(showServiceError); 
                  }) 
            }, showServiceError); 
         }); 
      }) 
      .fail(showServiceError); 
 
   saveToCache = (document, done) => { 
      lt.Document.DocumentFactory.saveToCache(document) 
         .done(done) 
         .fail(showServiceError); 
   } 
 
   loadFromCache = (id, callback, fail) => { 
      lt.Document.DocumentFactory.loadFromCache(id) 
         .done(callback) 
         .fail(fail); 
   } 
 
   addUserTokenSupport = () => { 
      log("User token support added"); 
      _sendUserToken = true; 
   } 
 
   removeUserTokenSupport = () => { 
      log("User token support removed"); 
      _sendUserToken = false; 
   } 
 
   shouldntSucceedCallback = () => { 
      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"] = _userToken; 
   } 
 
   prepareAjaxListener = (sender, e) => { 
      if (_sendUserToken) 
         prepareAjaxUserToken(sender, e); 
 
      if (e.settings.headers) { 
         log("User Token header value: " + e.settings.headers["user-token"]); 
         log("Header dump available in console", e.settings.headers); 
         return; 
      } 
 
      log("No headers sent."); 
   } 
class UserToken_Example { 
    private _userToken = "my-secret"; 
    private _sendUserToken = false; 
 
    public constructor() { 
        lt.RasterSupport.setLicenseUri("https://demo.leadtools.com/licenses/v200/LEADTOOLSEVAL.txt", "EVAL", null); 
    } 
 
    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(); 
 
        log("Loading a document with a user token"); 
        lt.Document.DocumentFactory.loadFromUri(imageUrl, loadDocumentOptions) 
        .done((doc: lt.Document.LEADDocument) => { 
            log("Document loaded"); 
 
            // Remove user token support 
            this.removeUserTokenSupport(); 
 
            // Try to load the file without a user-token 
            // This should fail 
            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 
                    log("Did load fail: " + !doc, doc); 
 
                    // Remove token support and try and delete the doc 
                    this.removeUserTokenSupport(); 
                    lt.Document.DocumentFactory.deleteFromCache(docId) 
                        .done(() =>{ 
                            log("Document shouldn't have been deleted"); 
                        }) 
                        .fail(() => { 
                            log("Failed to delete Document"); 
                            // Add token support back, and try to delete again. 
                            this.addUserTokenSupport(); 
                            lt.Document.DocumentFactory.deleteFromCache(docId) 
                                .done(() => { 
                                    log("Document successfully deleted"); 
                                }) 
                                .fail(showServiceError); 
                        }) 
                }, showServiceError); 
            }); 
        }) 
        .fail(showServiceError); 
    } 
 
    saveToCache = (document: lt.Document.LEADDocument, done: () => void) => { 
        lt.Document.DocumentFactory.saveToCache(document) 
            .done(done) 
            .fail(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 = () => { 
        log("User token support added"); 
        this._sendUserToken = true; 
    } 
 
    removeUserTokenSupport = () => { 
        log("User token support removed"); 
        this._sendUserToken = false; 
    } 
 
    shouldntSucceedCallback = () => { 
        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) { 
            log("User Token header value: " + e.settings.headers["user-token"]); 
            log("Header dump available in console", e.settings.headers); 
            return; 
        } 
 
        log("No headers sent."); 
    } 
 
} 

Requirements

Target Platforms

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

Leadtools.Document Assembly