热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用javascript创建登录代码-creatingsign-incodewithjavascript

Iamdevelopingasign-upsystemwheretheuserscancreatearandomstringbyjusthittingabutton

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.

为了实现这个场景,我正在寻找一个可以创建随机字符串的Javascript函数。您可以将字符串提供给该函数,并且可以识别它是否创建它 - 引发错误。

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.

这是一组封闭的后端用户。我已经是会员了。我可以通过电子邮件,Skype,fb聊天等方式创建一个字符串并发送给我的朋友杰克。我告诉他,“输入你想要的任何用户名/密码,但把我发送给你的代码放到注册表单的'你的代码'字段中,否则系统不会让你注册”。 Jake注册后,字符串不再有用,因为Jake创建了自己的密码,系统使用该密码进行识别。这就是场景。

I insist on Javascript because I am going to use node.js/express.js , so I guess whatever I make , it have to be written in Javascript.

我坚持使用Javascript,因为我将使用node.js / express.js,所以无论我做什么,它都必须用Javascript编写。

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

我想我可以使用像php b-crypt的Javascript版本这样的东西。或不。我甚至不知道如何开始,所以任何指南都会有用。谢谢

1 个解决方案

#1


2  

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.

    请求/ generate URL在服务器上生成随机字符串,将该字符串存储在服务器上的活动代码数组中,然后返回该字符串。

  • 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).

    / register路由将包含两部分:GET路径将返回一个HTML表单,您的朋友必须填写他的代码(您已通过电子邮件发送),他的新用户名和新密码。 POST路径将此数据发送到服务器,并使用存储在活动代码数组中的代码检查其代码。如果他的代码与数组中的某个代码匹配,则会为用户创建他们在HTML表单中输入的凭据(否则会返回错误,例如无效代码错误)。

  • 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).

    您最好创建一个HTML页面来请求/生成URL。这将包括一个附加函数onclick的按钮,该按钮使XMLHttpRequest与此URL异步。然后它会在

    标签中显示返回的代码(或任何浮动你的船)。

  • Naturally you'd also be required to create an HTML form at the /register route, for the friend to register his new account using the code you emailed.

    当然,您还需要在/ register路径创建HTML表单,以便朋友使用您通过电子邮件发送的代码注册其新帐户。

  • 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.

    您需要某种数据库来存储您的用户凭据。它可能像.json文件一样简单,也可能是更复杂的NOSQL数据库。我不会详细介绍如何实现这一点,但是无论您采用哪种实现方式,都存在许多优秀的资源。

Here is a basic outline for the code you'd write:

以下是您编写的代码的基本大纲:

routes/index.js - this would be in your express app

routes / index.js - 这将在你的快递应用程序中

var activeKeys = [];

router.get("/", function(req, res) {
    var optiOns= {
        root: __dirname + '/public/',
        dotfiles: 'deny',
        headers: {
            'sent-timestamp': Date.now()
        }
    };

    res.sendFile("index.html", options, function (err) {
        if (err) {
            console.log(err);
            res.status(err.status).end();
        }
        else {
            return;
        }
    });
});

router.get("/generate", function(req, res) {
    require('crypto').randomBytes(48, function(ex, buf) {
        var token = buf.toString('hex');
        activeKeys.push(token);
        res.send(token);
        return;
    });
});

router.get("/register", function(req, res) {    
    var optiOns= {
        root: __dirname + '/public/',
        dotfiles: 'deny',
        headers: {
            'sent-timestamp': Date.now()
        }
    };

    res.sendFile("register.html", options, function (err) {
        if (err) {
            console.log(err);
            res.status(err.status).end();
        }
        else {
            return;
        }
    });
});

router.post("/register", function(req, res) {
    var validKey = keys.indexOf(req.body.code);
    if (validKey <0) {
        res.send("invalid code error");
    }
    else {
        //here you would implement adding your newly created user to the database,
        //and then sending a success response
    }
});

public/index.html - default route, where the user

public / index.html - 默认路由,用户所在的位置


Mail this to your friend!

public/register.html - your friend registers his new account here

public / register.html - 您的朋友在这里注册他的新帐户


    
    
    
    

That should just about suit your needs.

那应该只是满足你的需求。

PLEASE NOTE: I haven't tested any of this code, so don't expect to copy + paste and for it to work. This is meant only as a guideline

请注意:我没有测试任何此代码,所以不要期望复制+粘贴并使其工作。这仅作为指导

Hope this helps!

希望这可以帮助!


推荐阅读
author-avatar
Only-安之若素
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有