Create a random token in Javascript based on user details

后端 未结 6 820
醉酒成梦
醉酒成梦 2021-02-01 15:38

I want to create a random string (token) which can be used to identify a user whilst avoiding any potential conflicts with any other users\' tokens.

What I was thinking

6条回答
  •  长发绾君心
    2021-02-01 16:02

    You could generate a random number and convert it to base 36 (0-9a-z):

    var rand = function() {
        return Math.random().toString(36).substr(2); // remove `0.`
    };
    
    var token = function() {
        return rand() + rand(); // to make it longer
    };
    
    token(); // "bnh5yzdirjinqaorq0ox1tf383nb3xr"
    

提交回复
热议问题