Users.js 1.18 KB
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = require("../libs/MySQL");
const usersRouter = express.Router();

async function route() {
  const connection = await mysql2.createConnection(connectOption);

  // (loginID) => null | ID(str)
  usersRouter.get("/", async (req, res) => {
    console.log("/db/users");
    try {
      const queryString = `
          SELECT ID FROM users us
            WHERE us.loginID = '${req.query.loginID}'`;
      const [results] = await connection.query(queryString);
      res.send(results.length ? results[0].ID.toString() : null);
    } catch (e) {
      console.log(e);
      res.end();
    }
  });

  // (loginID, loginPW) => null | "correct"
  usersRouter.get("/check", async (req, res) => {
    console.log("/db/users/check");
    try {
      const queryString = `
          SELECT loginPW FROM users us
            WHERE us.loginID = '${req.query.loginID}'`;
      const [results] = await connection.query(queryString);
      res.send(results[0].loginPW === req.query.loginPW ? "correct" : null);
    } catch (e) {
      console.log(e);
      res.end();
    }
  });
}
route();

module.exports = usersRouter;