CZ_OpenSpice/handler/auth.py

69 lines
2.6 KiB
Python
Raw Normal View History

2021-01-20 12:01:46 +00:00
from .main import AuthBaseHandler
from .account import authenticate, add_user
2021-01-22 12:09:14 +00:00
from .MongoDB import *
2021-01-20 12:01:46 +00:00
class LoginHandler(AuthBaseHandler):
def get(self,*args,**kwargs):
2021-01-22 12:09:14 +00:00
self.render('auth/login.html')
2021-01-20 12:01:46 +00:00
def post(self,*args,**kwargs):
username = self.get_argument('username',None)
password = self.get_argument('password',None)
# check the password
passed = authenticate(username,password)
if passed:
# 保存cookie信息到redis数据库
self.session.set('username',username) #将前面设置的cookie设置为username保存用户登录信息
2021-01-22 12:09:14 +00:00
print(self.session.get('username')+' login success !!!')
2021-01-20 12:01:46 +00:00
next_url = self.get_argument('next', '') # 获取之前页面的路由
if next_url:
2021-01-22 12:09:14 +00:00
Mongo.connect(DataBase='example',Collection=username)
Mongo.update(behavior='login',tags='auth')
2021-01-20 12:01:46 +00:00
self.redirect(next_url) #跳转主页路由
else:
2021-01-22 12:09:14 +00:00
Mongo.connect(DataBase='example',Collection=username)
Mongo.update(behavior='login',tags='auth')
2021-01-20 12:01:46 +00:00
self.redirect('/')
else:
self.write({'msg':'login fail'}) #不通过,有问题
class LogoutHandler(AuthBaseHandler):
def get(self, *args, **kwargs):
2021-01-22 12:09:14 +00:00
Mongo.connect(DataBase='example',Collection=self.get_current_user())
Mongo.update(behavior='logout',tags='auth')
2021-01-20 12:01:46 +00:00
#self.session.set('username','') #将用户的cookie清除
self.session.delete('username')
self.redirect('/login')
# def post(self,*args,**kwargs):
# self.session.delete('username')
# self.redirect('/login')
class RegisterHandler(AuthBaseHandler):
def get(self, *args, **kwargs):
print('register')
2021-01-22 12:09:14 +00:00
self.render('auth/register.html')
2021-01-20 12:01:46 +00:00
def post(self, *args, **kwargs):
print('registerpost')
username = self.get_argument('username','')
2021-01-26 12:47:02 +00:00
email = self.get_argument('email','')
2021-01-20 12:01:46 +00:00
password1 = self.get_argument('password1','')
password2 = self.get_argument('password2','')
if username and password1 and (password1 == password2):
2021-01-26 12:47:02 +00:00
success = add_user(username,password1,email)
2021-01-20 12:01:46 +00:00
if success:
2021-01-22 12:09:14 +00:00
Mongo.connect(DataBase='example',Collection=username)
Mongo.update(behavior='register',tags='auth')
2021-01-20 12:01:46 +00:00
self.redirect('/login')
else:
self.write({'msg':'register fail'})
else:
print('register again')
2021-01-22 12:09:14 +00:00
self.render('auth/register.html')