Attendance/app.py

93 lines
3 KiB
Python
Raw Normal View History

2021-09-09 06:29:31 -07:00
from flask import *
import pyrebase
from datetime import datetime
import pytz
import os
from dotenv import load_dotenv
2021-09-23 07:21:59 -07:00
import requests
2021-09-26 06:37:34 -07:00
from manage import manage
from upload import upload
load_dotenv()
2021-09-09 06:29:31 -07:00
app = Flask(__name__)
2021-09-26 06:37:34 -07:00
app.register_blueprint(manage)
app.register_blueprint(upload)
2021-09-09 06:29:31 -07:00
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
config = {
"apiKey": os.environ.get('apiKey'),
"authDomain": os.environ.get('authDomain'),
"databaseURL": os.environ.get('databaseURL'),
"storageBucket": os.environ.get('storageBucket'),
"serviceAccount": os.environ.get('serviceAccount'),
"messagingSenderId": os.environ.get('messagingSenderId'),
"appId": os.environ.get('appId'),
"measurementId": os.environ.get('measurementId'),
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
tz = pytz.timezone('Asia/Taipei')
def check_login_status():
return ('is_logged_in' not in session or
session['is_logged_in'] == False or
(datetime.now(tz) - session['loginTime']).total_seconds() > 3600)
2021-09-23 07:21:59 -07:00
def verify_recaptcha(response):
2021-10-01 19:38:28 -07:00
return True
2021-09-23 07:21:59 -07:00
data = {
'secret': os.environ.get('RECAPTCHA_SECRET'),
'response': response,
'remoteip': request.remote_addr
}
r = requests.post(
'https://www.google.com/recaptcha/api/siteverify', data=data)
2021-10-01 18:57:36 -07:00
print(j.json())
2021-09-23 07:21:59 -07:00
return r.json()['success']
2021-09-24 22:31:36 -07:00
@ app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
if check_login_status():
2021-09-24 22:31:36 -07:00
return render_template('login.html')
return redirect('/manage')
elif request.method == 'POST':
2021-10-01 08:08:10 -07:00
email = request.form['username'] + "@group-attendance.fhjh.tp.edu.tw"
if check_login_status():
try:
2021-10-01 19:38:28 -07:00
if (verify_recaptcha("")):
2021-09-23 07:21:59 -07:00
user = auth.sign_in_with_email_and_password(
2021-10-01 08:08:10 -07:00
email, request.form['password'])
print("Login SUCC:", email, flush=True)
2021-09-23 07:21:59 -07:00
session['is_logged_in'] = True
session['email'] = user['email']
session['uid'] = user['localId']
session['token'] = user['idToken']
session['refreshToken'] = user['refreshToken']
session['loginTime'] = datetime.now(tz)
return redirect('/manage')
else:
2021-10-01 08:08:10 -07:00
print("ReC Error:", email, flush=True)
2021-09-24 22:31:36 -07:00
flash(
'reCAPTCHA 錯誤,請稍後再試一次<br>reCAPTCHA Failed. Please try again later.')
return redirect('/')
except Exception as e:
2021-10-01 08:10:07 -07:00
print("Error:", email, str(e), flush=True)
flash(
2021-09-24 22:31:36 -07:00
'帳號或密碼錯誤,請重新輸入<br>Incorrect username or password')
2021-10-01 08:10:07 -07:00
return redirect('/')
else:
return redirect('/manage')
2021-09-12 02:39:09 -07:00
@ app.route('/logout', methods=['GET'])
2021-09-09 06:29:31 -07:00
def logout():
session.clear()
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)