2

I'm trying to create a login request for my server. What am I trying to learn is How to create a token when the user logged in. After I created a token, I want to control user's requests with that token. Here are my code's examples. Please tell me where am I going wrong. If the all way is true, can you explain to me how to create it?

    var port = 8080;
var express = require("express");
var bodyParser = require('body-parser');
var mysql = require('mysql');
var bcrypt = require('bcrypt');
var session = require('express-session');

var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "123456789",
    database: "circles",
    debug: false
});

process.on('uncaughtException', function (err) {
    console.log('UNCAUGHT', err.stack);
});

var app = express();
app.use(express.static("localhost" + "/public")); //use static files in ROOT/public folder
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({secret: 'letsputasmile',
                proxy: true,
                resave: true,
                cookie: { maxAge : 2628000000 },
                saveUninitialized: true
                }));

app.get("/", function(request, response){ //root dir
    response.send("Hello!!");
    console.log(response);
});



app.post("/register", function (request, response) {
    var name = request.body.name;
    var username = request.body.username;
    var pass = request.body.pass;
    var salt = bcrypt.genSaltSync(10);
    var hash = bcrypt.hashSync(pass,salt);
    console.log(name);
    console.log(username);
    console.log(pass);
    con.query("Insert Into user(name,username,password) Values(\"" + name + "\"," + "\"" + username + "\"," + "\"" + hash + "\")",
        function (err,result) {
        if(err) response.send("nop");
        else response.send("success");
        });
});

app.post("/login", function(request, response) {
    var username = request.body.username;
    var pass = request.body.pass;
    var passer;

    con.query("select password from user where username = " + "\"" + username + "\"",function (err, result) {
        if(err) throw err;
        if(result.length === 1) {
            passer = result[0].password;
        }
        bcrypt.compare(pass, passer, function (err, res) {
            if(res) response.send("success");
            else response.send("nop");
        });
    });
});

app.listen(port);
4
  • What is session in this case? Please update your code. Commented Jul 29, 2017 at 12:53
  • @ralphtheninja I uploaded everything. I think I couldn't understand the session part Commented Jul 29, 2017 at 13:09
  • but what are you trying to accomplish? you need a token to use it on another app?, or a token to use it inside the same webapp? Commented Jul 29, 2017 at 13:14
  • @SebastiánEspinosa I'm gonna use it on another app(an android app) Commented Jul 29, 2017 at 13:20

1 Answer 1

1

What you could do is to create a jwt (json webtoken). You basically sign some payload (whatever meta data you want to give to the client) and the client can then pass the token back when requesting something and the server can verify the token (symmetric key) if it's correct and also parse out data the server needs, e.g. user_id or similar.

Check https://github.com/auth0/node-jsonwebtoken for more information

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.