#routes/login.py
from bottle import Bottle,redirect
from controllers.login import checkLogged

app = Bottle()

@app.route('/')
def getForm():
    if checkLogged.call():
        redirect('/admin/post')
    else:
        from controllers.login import get
        return get.call()

@app.route('/',method='post')
def checkUser():
    from controllers.login import checkUser
    return checkUser.call()

 

#controllers/login/checkLogged.py
import config
from bottle import request

def call():
    user = request.get_cookie('userID', secret=config.kdict['SECRET_KEY'])
    return user

 

#controllers/login/checkUser.py
import config,bcrypt
from copy import deepcopy
from bottle import template, request, response, redirect
from models.users import checkUser
 
def call():
    kdict = deepcopy(config.kdict)
    kdict['pageTitle'] = 'ទំព័រ​ចុះ​ឈ្មោះ'
 
    password = request.forms.getunicode('password')
    email = request.forms.getunicode('email')
    
    user = checkUser.call(email)
    
    if user:
        if(bcrypt.checkpw(password.encode('utf-8'), user['password'])):
            kdict["pageTitle"] = 'ទំព័រ​ការផ្សាយ'
            kdict['route'] = '/admin/post'
            response.set_cookie('userID', user['userID'], path='/', secret=kdict['SECRET_KEY'])
            response.set_cookie('userRole', user['role'], path='/', secret=kdict['SECRET_KEY'])
            return redirect('/admin/post')
        else:
            kdict['message'] = 'ពាក្យ​សំងាត់របស់អ្នក​​មិន​ត្រឹមត្រូវ​ទេ!'
            kdict['route'] = '/login'
            return template('base', data=kdict)
    else:
        kdict['message'] = 'Email របស់​អ្នក​មិន​ត្រឹមត្រូវ​ទេ!'
        kdict['route'] = '/login'
        return template('base', data=kdict)

 

#models/users/checkUser.py
import config
from .. import setDBconnection
 
def call(email):
    users_ref = setDBconnection.db.collection(u'users').where(u'email',u'==',email)
    users = users_ref.stream()

    for user in users:
        userEmail = user.get('email')
        if(userEmail == email):
            return user.to_dict()

 

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

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