// routes/fron/login.js
import { Router } from "../../deps.ts"
const router = Router()
import login from '../../controllers/front/login.jsx'
router.get('/', async (req, res) => {
if(await req.session.has('user')){
res.redirect('/admin/post')
}else{
login.getItem(req, res)
}
})
router.post('/', async (req, res) => {
login.checkUser(req, res)
})
export default router
// controllers/front/login.jsx
/** @jsx h */
import { h, renderSSR } from "../../deps.ts"
import config from '../../config.js'
import Login_ from '../../views/front/login.jsx'
import userdb from '../../models/users.ts'
import { bcrypt } from '../../deps.ts'
class Login{
async getItem(req, res){
this.config = await config()
this.config.pageTitle = 'ទំព័ចូលក្នុង'
this.config.route = '/login'
const str = renderSSR(<Login_ config={this.config} />)
const html = `<!DOCTYPE html>${str}`
res.send(html)
}
async checkUser(req,res){
this.config = await config()
this.config.pageTitle = 'ផ្ទៀងផ្ទាត់ពាក្យសំងាត់'
let user = await userdb.checkUser(req)
if(user){
if(user.role in {'Admin':1,'Editor':1,'Author':1}){
if(await bcrypt.compareSync(req.body.password, user.password)){
await req.session.set("user", user)
res.redirect('/admin/post')
}else{
this.config.message = 'ពាក្យសំងាត់មិនត្រឹមត្រូវទេ'
this.config.route = '/login'
const str = renderSSR(<Login_ config={this.config} />)
const html = `<!DOCTYPE html>${str}`
res.send(html)
}
}else if(user.role in {'subscriber':1}){
this.config.message = 'សូមចុចបញ្ជាក់ការចុះឈ្មោះក្នុង Email របស់អ្នក'
this.config.route = '/login'
const str = renderSSR(<Login_ config={this.config} />)
const html = `<!DOCTYPE html>${str}`
res.send(html)
}
}else{
this.config.message = 'Email មិនត្រឹមត្រូវទេ'
this.config.route = '/login'
const str = renderSSR(<Login_ config={this.config} />)
const html = `<!DOCTYPE html>${str}`
res.send(html)
}
}
}
export default new Login()
GitHub: https://github.com/Sokhavuth/khmerweb-deno
Deno Deploy: https://khmerweb-blog.deno.dev/login