-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (53 loc) · 1.97 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from datetime import timedelta
from flask import Flask, request, redirect, url_for, render_template, flash, session
from flask_login import LoginManager, login_user, logout_user, current_user, login_required
from models import User, query_user
from form import LoginForm
from dotenv import load_dotenv
import os
load_dotenv(encoding='utf8')
app = Flask(__name__)
app.secret_key = os.getenv("APP_SECRET_KEY")
app.config['RECAPTCHA_USE_SSL'] = True
app.config['RECAPTCHA_PUBLIC_KEY'] = os.getenv("RECAPTCHA_PUBLIC_KEY")
app.config['RECAPTCHA_PRIVATE_KEY'] = os.getenv("RECAPTCHA_PRIVATE_KEY")
app.config['RECAPTCHA_OPTIONS'] = {'theme': 'black'}
login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.init_app(app)
# login_manager.remember_cookie_duration = timedelta(minutes=1)
@login_manager.user_loader
def load_user(user_id):
if query_user(user_id) is not None:
curr_user = User()
curr_user.id = user_id
return curr_user
@app.route('/', methods=['GET', 'POST'])
@login_required
def index():
# return 'Logged in as: %s' % current_user.get_id()
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user_id = form.name.data
user = query_user(user_id)
if user is not None and form.password.data == user['password']:
curr_user = User()
curr_user.id = user_id
# 通过Flask-Login的login_user方法登录用户
login_user(curr_user)
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=5)
return redirect(url_for('index'))
flash('Wrong username or password!')
# GET 请求
return render_template('login.html', form=form)
@app.route('/logout', methods=['GET', 'POST'])
@login_required
def logout():
logout_user()
return redirect("/login")
if __name__ == '__main__':
app.run(debug=True)