Create a random token in Javascript based on user details

后端 未结 6 823
醉酒成梦
醉酒成梦 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 15:53

    This function allows you to set the token length and allowed characters.

    function generate_token(length){
        //edit the token allowed characters
        var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
        var b = [];  
        for (var i=0; i

    Simply call generate_token

    generate_token(32); //returns "qweQj4giRJSdMNzB8g1XIa6t3YtRIHPH"
    

提交回复
热议问题