<%- include('partials/header.ejs') %>
<link href="/styles/login.css" rel="stylesheet"></link>
  
<section class='Login'>
    <div class='title'>ចុះ​ឈ្មោះ​ចូល​ទំព័រ​គ្រប់គ្រង</div>

    <form action='/users' method='post' >
        <a>Email:</a><input type='email' name='email' required />
        <a>ពាក្យ​សំងាត់ៈ</a><input type='password' name='password' required />
        <a></a><input type='submit' value='បញ្ជូន' />
        <a></a><div class='info'><%= message %></div>
    </form>
    <footer>&copy; 2021 Khmer Web</footer>
</section>
    
<%- include('partials/footer.ejs') %>

 

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


router.get('/', async function(req, res, next) {
  const settings = require('../settings')
  settings.message = ''

  if(req.session.user){
    res.render('users/index', settings)
  }else{
    res.render('login', settings)
  }
})

router.post('/', async function(req, res, next){
  const settings = require('../settings')
  const checkUser = require('../controllers/users/checkUser')
  const result = await checkUser(req)
  settings.message = result.message

  if(result.success){
    res.render('users/index', settings)
  }else{
    res.render('login', settings)
  }

})

module.exports = router

 

const schema = require('./schema')
const bcrypt = require('bcryptjs')

async function checkUser(req){
    const users = await schema()
    const result = {}
    
    try{
        var user = await users.findOne({email:req.body.email})
    }catch (err){
        console.log(err)
    }
      
    if(user){
      if(bcrypt.compareSync(req.body.password, user.password)){
        result.success = true
        req.session.user = user
        result.message = ''
        return result
      }else{
        result.message = 'ពាក្យ​សំងាត់​មិន​ត្រឹមត្រូវ​ទេ'
        result.success = false;
        return result
      }
    }else{
        result.message = 'Email មិន​ត្រឹមត្រូវទេ'
        result.success = false
        return result
    }

  }

module.exports = checkUser

 

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

Vercel: https://tv-neon.vercel.app

Responsive: http://www.responsinator.com