mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-09 01:07:27 +08:00
It will be released soon because lowiro will update in some days. Fix many bugs and I forgot them. :< I push this because there's a small but serious safety problem. It should be fixed immediately. Oh, and this update needs you to login in arcaea again because a new function is added.
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
#import sqlite3
|
|
from flask import (Blueprint, flash, g, redirect,
|
|
render_template, request, session, url_for)
|
|
import functools
|
|
from setting import Config
|
|
|
|
bp = Blueprint('login', __name__, url_prefix='/web')
|
|
|
|
|
|
@bp.route('/login', methods=('GET', 'POST'))
|
|
def login():
|
|
# 登录
|
|
if request.method == 'POST':
|
|
username = request.form['username']
|
|
password = request.form['password']
|
|
error = None
|
|
|
|
if username != Config.USERNAME or password != Config.PASSWORD:
|
|
error = '错误的用户名或密码 Incorrect username or password.'
|
|
|
|
if error is None:
|
|
session.clear()
|
|
session['user_id'] = Config.USERNAME + Config.PASSWORD
|
|
return redirect(url_for('index.index'))
|
|
|
|
flash(error)
|
|
|
|
return render_template('web/login.html')
|
|
|
|
|
|
@bp.route('/logout')
|
|
def logout():
|
|
# 登出
|
|
session.clear()
|
|
flash('成功登出 Successfully log out.')
|
|
return redirect(url_for('index.index'))
|
|
|
|
|
|
def login_required(view):
|
|
# 登录验证,写成了修饰器
|
|
@functools.wraps(view)
|
|
def wrapped_view(**kwargs):
|
|
x = session.get('user_id')
|
|
|
|
if x != Config.USERNAME + Config.PASSWORD:
|
|
return redirect(url_for('login.login'))
|
|
|
|
g.user = {'user_id': x, 'username': Config.USERNAME}
|
|
return view(**kwargs)
|
|
|
|
return wrapped_view
|