#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