@ -6,7 +6,7 @@ from app import db
from app . auth import bp
from app . auth import bp
from app . utils import generate_qr_code , serve_pil_image
from app . utils import generate_qr_code , serve_pil_image
from app . models import User
from app . models import User
from app . auth . forms import LoginForm , RegistrationForm
from app . auth . forms import LoginForm , RegistrationForm , ChangePasswordForm , SetPasswordForm
@bp . route ( ' /login ' , methods = [ ' GET ' , ' POST ' ] )
@bp . route ( ' /login ' , methods = [ ' GET ' , ' POST ' ] )
def login ( ) :
def login ( ) :
@ -94,3 +94,35 @@ def user_qrcode(auth_hash):
abort ( 403 )
abort ( 403 )
img = generate_qr_code ( url_for ( ' auth.user_hash_login ' , auth_hash = auth_hash , _external = True ) )
img = generate_qr_code ( url_for ( ' auth.user_hash_login ' , auth_hash = auth_hash , _external = True ) )
return serve_pil_image ( img )
return serve_pil_image ( img )
@bp . route ( ' /set_password ' , methods = [ ' GET ' , ' POST ' ] )
@bp . route ( ' /change_password ' , methods = [ ' GET ' , ' POST ' ] )
def change_password ( ) :
auth_hash = request . args . get ( ' auth_hash ' , default = None , type = str )
if auth_hash :
user = User . query . filter_by ( auth_hash = auth_hash ) . first_or_404 ( )
login_user ( user , True )
user . last_login = datetime . utcnow ( )
else :
if not current_user . is_authenticated :
abort ( 403 )
user = User . query . filter_by ( name = current_user . name ) . first_or_404 ( )
no_old_password = not user . password_hash
if no_old_password :
form = SetPasswordForm ( )
else :
form = ChangePasswordForm ( )
if form . validate_on_submit ( ) :
if not no_old_password :
if not user . check_password ( form . old_password . data ) :
flash ( ' Invalid password ' )
return redirect ( url_for ( ' auth.change_password ' ) )
user . set_password ( form . new_password . data )
db . session . commit ( )
if no_old_password :
flash ( ' Your password was set ' )
else :
flash ( ' Your password was changed! ' )
return redirect ( url_for ( ' main.index ' ) )
return render_template ( ' auth/change_password.html ' , form = form )