Added login persistence.

This commit is contained in:
neviyn 2015-12-24 17:03:54 +00:00
parent 441623a947
commit eba6c19f6e

View File

@ -3,7 +3,8 @@ import re
from datetime import datetime, timedelta from datetime import datetime, timedelta
import flask import flask
from flask.ext.login import LoginManager, login_user, login_required, current_user, logout_user, login_fresh from flask.ext.login import LoginManager, login_user, login_required, current_user, logout_user, login_fresh, \
confirm_login
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.wtf import Form from flask.ext.wtf import Form
import requests import requests
@ -161,12 +162,12 @@ def show_home():
if form.validate_on_submit(): if form.validate_on_submit():
user = User.query.get(str(form.api_key.data)) user = User.query.get(str(form.api_key.data))
if user: if user:
login_user(user) login_user(user, remember=True)
return flask.redirect(flask.url_for('show_quiz')) return flask.redirect(flask.url_for('show_quiz'))
else: else:
try: try:
new_user = User(str(form.api_key.data)) new_user = User(str(form.api_key.data))
login_user(new_user) login_user(new_user, remember=True)
return flask.redirect(flask.url_for('show_quiz')) return flask.redirect(flask.url_for('show_quiz'))
except ValueError as err: except ValueError as err:
flask.flash(err) flask.flash(err)
@ -179,6 +180,7 @@ def show_home():
def show_quiz(): def show_quiz():
if not login_fresh(): if not login_fresh():
if User.query.get(current_user.api_key): if User.query.get(current_user.api_key):
confirm_login()
return flask.render_template("quiz.html") return flask.render_template("quiz.html")
else: else:
logout_user() logout_user()