I am developing a sign-up system where the users can create a random string by just hitting a button.
我正在开发一个注册系统,用户只需按一下按钮即可创建一个随机字符串。
They can then send this string to a non-user so s/he can use it to sign-up. The new user inserts a username and a password of his/hers choice and the string s/he received.
To implement this scenario, I am looking for a Javascript function that can create a random string. You can feed a string to the function and can recognize if it created it or not -throws an error.
Any tips for creating such a system and where can I find such a function?
有关创建此类系统的任何提示,我在哪里可以找到这样的功能?
Thanks
Edit
This is a closed group of back-end users. I am already a member. I can create a string and send to my friend Jake by e-mail, skype, fb chat, whatever. I tell him, "put whatever username/password you want, but put the code I send you to the 'Your Code' field of the signup form, otherwise the system wont let you sign up". After Jake signs up, the string is no longer useful, because Jake created his own password and that is used by the system for identification. That's the scenario.
I guess I could use something like the Javascript version of php b-crypt if this exists. Or not. I dont even know how to begin, so any guidelines would be useful. Thanks
Okay, since you've specified you'd like to use express, here's something like a solution to your problem.
好的,既然你已经指定你想使用快递,这里就像你的问题的解决方案。
Firstly, you wanted a way of generating a random string which could be identifiable by the function that created it - which is impossible. Instead, you need the node application to remember the strings that have been generated by this function. You can go about this a number of ways but here is the process I will be outlining:
You create a simple express API with a couple of routes: /generate, and /register.
您可以使用以下几个路径创建一个简单的快速API:/ generate和/ register。
requesting the /generate URL generates a random string on the server, stores that string in an active codes array on the server, and then returns that string.
The /register route will have two parts: the GET path will return an HTML form that your friend will have to fill in with his code (which you have emailed), his new username, and his new password. The POST path will send this data to the server, and check his code with the codes stored in the active codes array. If his code matches one of the codes in the array, this creates a user with the credentials they entered in the HTML form (otherwise it returns an error, something like invalid code error).
Preferably, you'd want to create an HTML page for requesting the /generate URL. This would include a button that attaches a function onclick that makes an XMLHttpRequest asynchronously to this URL. It then displays the returned code in a tag (or whatever floats your boat).
You'll need some kind of database to store your user credentials. It could be something as simple as a .json file, or a more sophisticated NOSQL database. I won't go into the details of implementing this, but a number of great resources exist out there for whichever implementation you go with.