{"size": 1141, "ext": "js", "max_stars_repo_path": "routes/login.js", "max_stars_repo_name": "siveroo/retail-web", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/login.js", "max_issues_repo_name": "siveroo/retail-web", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/login.js", "max_forks_repo_name": "siveroo/retail-web", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.8292682927, "max_line_length": 100, "alphanum_fraction": 0.5766871166}
const { Router } = require("express");
const path = require("path");
const express = require("express");
const { dbIsEmailRegistered, dbAuthUser, dbGetIDByEmail } = require("../Util/DatabaseHandler/User");

const router = Router();

router.use("/", express.static(path.join(__dirname, '../public/login')));

router.post("/", async(req, res) => {
    const { email, password } = req.body;
    try {
        if(!(email && password)){
            res.status(400).send("Data pengguna tidak valid!");
            return;
        }

        const isRegistered = await dbIsEmailRegistered(email);
        if(!isRegistered){
            res.status(400).send("Email tidak ditemukan!");
            return;
        }

        const isAuth = await dbAuthUser(email, password);
        //console.log(isAuth);
        if(!isAuth){
            res.status(400).send("Password salah!");
            return;
        }

        const user_id = await dbGetIDByEmail(email);
        req.session.isAuth = true;
        req.session.user_id = user_id;
        res.status(200).end();

    } catch (err) {
        console.log(err);
    }
})

module.exports = router;