본문 바로가기

Programing/node js

node js mysql 연동 암호화 로그인

 

-mysql

 

npm install mysql --save
npm install crypto --save

 

db_info.js

module.exports = {

    local : {

        host				: "localhost", 
        port				: 3306, 
        user				: "root", 
        password			: "비밀번호", 
        database			: "my_db", 
        connectionLimit		: 5000, 
        waitForConnection	: true,
        multipleStatements	: true,
    },

    joo : {

        host				: "호스트", 
        port				: 3306, 
        user				: "아이디", 
        password			: "비밀번호", 
        database			: "my_db", 
        connectionLimit		: 5000, 
        waitForConnection	: true,
        multipleStatements	: true,
    },

}

 

 

app.js

var mysql = require("mysql");
var mysql_info = require('./config/db_info').joo;

pool = mysql.createPool({
  	host: mysql_info.host,
	port: mysql_info.port,
	user: mysql_info.user,
	password: mysql_info.password,
	database: mysql_info.database,
	connectionLimit: mysql_info.connectionLimit,
	waitForConnection: mysql_info.waitForConnection,
	multipleStatements: mysql_info.multipleStatements,
});

 

crypto 암호 모듈 이용

login.js

var express = require('express');
var router = express.Router();
var crypto = require('crypto');

router.post('/login', function(req, res, next) {

    var id = req.body.user_id;
    var pw = req.body.user_password;
    req.session.userInfo = id;
    console.log("req.session : ", req.session);
    console.log("req.session.userInfo :" , req.session.userInfo);

    var hex = "joospace";
    var sql = 'SELECT * FROM users';
  
    pool.getConnection(function(error, connection) {

        connection.query(sql, [id], function(err, results) {
            if(err) {
                console.log(err);
                connection.release();
            }
            console.log(results);

            if(results.length == 0) {
                res.render('/', {message: 'please check your id'});
                return;
            }

            var userPassword = results[0].user_password;

            crypto.pbkdf2(pw, hex, 10000, 8, 'sha512', function(err, derivedkey) {
                if(err) {
                    console.log(err);
                    connection.release();
                }

                if(derivedkey.toString('hex') === userPassword) {
                    res.render('main', {id: id});
                }

                else {
                    res.render('/', {message: 'please check your password'});
                }
            });

            connection.release();
        })
    });

});

module.exports = router;

index.ejs

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <!-- jQury-->
    <script
    src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
    crossorigin="anonymous">
    </script>
    <title>Login Test</title>
</head>

<body>
    <form action="/login" method="POST">
        <div>
            <input type="text" id="user_id" name="user_id" autocomplete="off" placeholder="admin" value="">
            <label for="user_id">Email</label>
        </div> 

        <div>
            <input type="password" id="user_password" name="user_password" autocomplete="off" placeholder="**********" value="">
            <label for="user_password">Password</label>
        </div>
        <div>
            <input type="submit"></button>
        </div>
    </form>
</body>

</html>

 

npm start...

실행화면

 

main 페이지 이동 후 {id} 출력