127.0.0.1:8000 smart-home-server / master api_keygen.js
master

Tree @master (Download .tar.gz)

api_keygen.js @masterraw · history · blame

require('dotenv').config();
const mysql = require('mysql');

const connection = mysql.createConnection({
    connectionLimit: 10,
    host: 'localhost',
    user: process.env.SQL_USER,
    password: process.env.SQL_PASS,
    database: 'smart_home'
});

const user_id = process.argv[2].toLowerCase();

const crypto = require('crypto');
crypto.randomBytes(32, function(err, buf) {
    if (err) throw err;
    const key = buf.toString('hex');

    connection.query("UPDATE users SET api_key=? WHERE user_id=?", [key, user_id], function(error) {
        if (error) throw error;
        console.log("key updated", key);
    });

    connection.end();
});