Create a random token in Javascript based on user details

后端 未结 6 822
醉酒成梦
醉酒成梦 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:56

    //length: defines the length of characters to express in the string
    
    const rand=()=>Math.random(0).toString(36).substr(2);
    const token=(length)=>(rand()+rand()+rand()+rand()).substr(0,length);
    
    console.log(token(40));
    //example1:  token(10) => result: tsywlmdqu6
    //example2:  token(40) => result: m4vni14mtln2547gy54ksclhcv0dj6tp9fhs1k10

提交回复
热议问题