//routes/admin/login.js
import express from 'express'
const login = express.Router()

login.get('/',async function(req,res) {
    let module = await import('../../controllers/admin/login/get.js')
    module.default(res)
})

login.post('/',async function(req,res){
    const module = await import('../../controllers/admin/login/post.js')
    module.default(req,res)
})

export default login

 

//controllers/admin/login/post.js
import settings from '../../../settings.js'
import checkUser from '../../../models/users/checkUser.js'

export default async (req,res)=>{
    let user = await checkUser(req)
      
    if(user){
        if(user.password == req.body.password){
            req.session.user = user.userid
            res.redirect('/admin/post')
        }else{
            settings.message = 'ពាក្យ​សំងាត់​មិន​ត្រឹមត្រូវ​ទេ'
            settings.route = '/admin/login'
            res.render('base',settings)
        }
    }else{
        settings.message = 'Email មិន​ត្រឹមត្រូវទេ'
        settings.route = '/admin/login'
        res.render('base',settings)
    }
}

 

//models/users/checkUser.js

export default async (req)=>{
    const query = {email:req.body.email}
    let user = await req.mydb.collection("users").findOne(query)
    return user
}

 

GitHub: https://github.com/Sokhavuth/Blog_

Vercel: https://khmerweb-blog.vercel.app/