Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:32:17+00:00 2026-06-10T06:32:17+00:00

I am generating a token using hat. I am trying to write very paranoid

  • 0

I am generating a token using “hat”.

I am trying to write very paranoid code. So, the idea is that the system generates an ID, checks if it’s already taken (querying the DB), and if it hasn’t, it returns it. If it has, tries again, 5 times. After 5 times, something very strange is going on and the application throws an error.

The short question is: how can I make this code run 5 times, in sequence, with the option of calling the passed callback (see, “quit” the cycle) if the token is actually available?

Is is the code that only attempts once:

var hat = require('hat'),
mongoose = require('mongoose');

exports.makeToken = function( callback ){

  Workspace = mongoose.model("Workspace");

  var found = false;
  var token;

  // Generate the token, check that it's indeed available
  token = hat();
  Workspace.findOne( { 'access.token':token } , function(err, doc){
    if(err){
      callback(err, null);
    } else {
      if( doc ){
        callback( new Error("Cannot generate unique token"), null );
      } else {
        callback(null, token );
      }
    }
  });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:32:18+00:00Added an answer on June 10, 2026 at 6:32 am

    Unfortunately, I cannot use the ObjectId because 1) The user might decide to regenerate the token key 2) Users are not stored as individual documents.

    So, there is a chance that the token generated is not unique, which would give me a situation where there is the same token ID for two user<->workspace pair, which wouldn’t be good. Hence, the 5 attempts. I ADMIT that chances are pretty slim, but still…

    I came up with this (recursion is needed here):

    var hat = require('hat'),
    mongoose = require('mongoose');
    
    exports.makeToken = function( callback ){
    
      var attempts = 0;
      look();
    
      function look(){
        Workspace = mongoose.model("Workspace");
    
        // Generate the token, check that it's indeed available
        var token = hat();
        Workspace.findOne( { 'access.token':token } , function(err, doc){
    
          // There is an error: throw it straight away
          if(err){
            callback(err, null);
          } else {
    
            // Token already there: either give up (too many attempts)...
            if( !doc ){
              attempts ++;
              if( attempts == 5 ){
                callback( new Error("Cannot generate unique token"), null );
              } else {
                look();
              }
            // ... or try again by calling this function again!
            } else {
              callback(null, token );
            }
          }
        });
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have homework that should use fork() and wait() system calls: 1) Write two
I am using two legged OAuth for Google Contact Data API and generating token
I'm generating two matrices using the following function (note some code is omitted): srand(2007);
I have some server code that is generating thumbnails when an image is uploaded.
I am trying to use one-time passwords that can be generated using Google Authenticator
When generating models from postgresql with ColdFusion Illudium Code generator the boolean values get
I'm generating json from a Rails app, and am filtering fields using the format.json
I'm a newbie. I'm using a browser plugin that hits my node server, and
I'm using Irony.net for generating a parse tree out of the source. Essentially I'm
I am using Facebook PHP SDK to authenticate the user. After generating the LoginUrl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.