<!--views/front/login.ejs-->
<link rel="stylesheet" href="/styles/front/login.css" />

<section class="Login">
    <div class="wrapper">
        <div class="title">ផ្ទៀងផ្ទាត់​ពាក្យ​សំងាត់​ចូល​គណនី​</div>
        <form action="/login" method="post">
            <a>Email:</a><input type="email" name="email" />
            <a>ពាក្យ​សំងាត់ៈ</a><input type="password" name="password" />
            <a></a><input type="submit" value="បញ្ជូន" />
            <a></a><div class="fb-login-button" data-width="100%" 
            data-size="large" data-button-type="login_with" 
            data-layout="default" data-auto-logout-link="false" 
            data-use-continue-as="false" 
            onlogin="checkLoginState()"
            ></div>
            <a></a><div class="info"><%= data.message %></div>
        </form>
    </div>
    <a class="signup" href="/signup">ចុះ​ឈ្មោះ​ចុះ​ផ្សាយលក់​ទំនិញ (signup)</a>
</section>

<script>
    function checkLoginState() {
        FB.getLoginStatus(function(response) {
            if(response.authResponse.accessToken){
                FB.api('/me',function(user) {
                    const fbuser = {
                        id: user.id,
                        name: user.name, 
                    }
                
                    $.post('/login/fbuser',fbuser,function(data,status){
                        if(data.message){
                            alert(data.message)
                            window.location.href = data.redirect
                        }else{
                            window.location.href = data.redirect
                        }
                    })
                })
            }else{
                alert('​​​​​​​មានបញ្ហា​មិន​អាច​ចុះ​ឈ្មោះ​បាន')
            }
        })
    }

    window.fbAsyncInit = function() {
        FB.init({
            appId      : '3245867309046703',
            cookie     : true,
            xfbml      : true,
            version    : 'v14'
        })
        
        FB.AppEvents.logPageView(); 
    }
  
    (function(d, s, id){
       var js, fjs = d.getElementsByTagName(s)[0]
       if (d.getElementById(id)) {return;}
       js = d.createElement(s); js.id = id
       js.src = "https://connect.facebook.net/en_US/sdk.js"
       fjs.parentNode.insertBefore(js, fjs)
     }(document, 'script', 'facebook-jssdk'))
  </script>

 

// routes/front/login.js
import express from "express"
const loginRouter = express.Router()
import login from '../../controllers/front/login.js'

loginRouter.get('/',async (req,res)=>{
    if(req.session.user){
        res.redirect('/admin/post')
    }else{
        login.getItem(req,res)
    }
})

loginRouter.post('/',async (req,res)=>{
    login.checkUser(req,res)
})

loginRouter.post('/fbuser',async (req,res)=>{
    login.checkFBuser(req,res)
})

loginRouter.get('/logout',async (req,res)=>{
    if(req.session.user){
        req.session.destroy()
        res.redirect('/')
    }else{
        res.redirect('/login')
    }
})

export default loginRouter

 

// controllers/front/login.js
import config from "../../config.js"
import bcrypt from 'bcryptjs'
import userdb from "../../models/user.js"

class Login{
    async getItem(req,res){
        this.config = await config()
        this.config.pageTitle = 'ផ្ទៀងផ្ទាត់​ពាក្យ​សំងាត់'
        this.config.route = '/login'

        res.render('base',{data:this.config})
    }

    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(bcrypt.compareSync(req.body.password, user.password)){
                    req.session.user = user
                    res.redirect('/admin/post')
                }else{
                    this.config.message = 'ពាក្យ​សំងាត់​មិន​ត្រឹមត្រូវ​ទេ'
                    this.config.route = '/login'
                    res.render('base',{data:this.config})
                }
            }else if(user.role in {'subscriber':1}){
                this.config.message = 'សូម​ចុច​បញ្ជាក់ការចុះ​ឈ្មោះ​ក្នុង Email របស់​អ្នក​'
                this.config.route = '/login'
                res.render('base',{data:this.config})
            }
        }else{
            this.config.message = 'Email មិន​ត្រឹមត្រូវទេ'
            this.config.route = '/login'
            res.render('base',{data:this.config})
        }

    }

    async checkFBuser(req,res){
        const user = await userdb.editItem(req,req.body.id)
        if(user){
            req.session.user = user
            const redirectUrl = req.protocol + "://" + req.get('host') + '/admin/post'
            res.json({redirect:redirectUrl})
        }else{
            const redirectUrl = req.protocol + "://" + req.get('host') + '/signup'
            res.json({message:'​​​​​​​​​​​​​​​​​​​​​​​​​​​​​អ្នកមិន​ទាន់​ចុះ​ឈ្មោះ​នៅ​ឡើយ​ទេ',redirect:redirectUrl})
        }
    }
}

export default new Login()

 

Heroku: https://khmerweb-sale.herokuapp.com