You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.9 KiB

  1. from flask import render_template, redirect, request, url_for, flash
  2. from flask_login import login_user, logout_user, login_required, current_user
  3. from . import auth
  4. from .. import db
  5. from ..models import User
  6. from ..email import send_email
  7. from .forms import LoginForm, RegistrationForm, ChangePasswordForm, \
  8. PasswordResetRequestForm, PasswordResetForm, ChangeEmailForm
  9. @auth.before_app_request
  10. def before_request():
  11. if current_user.is_authenticated:
  12. current_user.ping()
  13. if not current_user.confirmed \
  14. and request.endpoint \
  15. and request.blueprint != 'auth' \
  16. and request.endpoint != 'static':
  17. return redirect(url_for('auth.unconfirmed'))
  18. @auth.route('/unconfirmed')
  19. def unconfirmed():
  20. if current_user.is_anonymous or current_user.confirmed:
  21. return redirect(url_for('main.index'))
  22. return render_template('auth/unconfirmed.html')
  23. @auth.route('/login', methods=['GET', 'POST'])
  24. def login():
  25. form = LoginForm()
  26. if form.validate_on_submit():
  27. user = User.query.filter_by(email=form.email.data).first()
  28. if user is not None and user.verify_password(form.password.data):
  29. login_user(user, form.remember_me.data)
  30. next = request.args.get('next')
  31. if next is None or not next.startswith('/'):
  32. next = url_for('main.index')
  33. return redirect(next)
  34. flash('Invalid username or password')
  35. return render_template('auth/login.html', form=form)
  36. @auth.route('/logout')
  37. @login_required
  38. def logout():
  39. logout_user()
  40. flash('You have been logged out.')
  41. return redirect(url_for('main.index'))
  42. @auth.route('/register', methods=['GET', 'POST'])
  43. def register():
  44. form = RegistrationForm()
  45. if form.validate_on_submit():
  46. user = User(email=form.email.data,
  47. username=form.username.data,
  48. password=form.password.data)
  49. db.session.add(user)
  50. db.session.commit()
  51. token = user.generate_confirmation_token()
  52. send_email(user.email, 'Confirm your account',
  53. 'auth/email/confirm', user=user, token=token)
  54. flash('A confirmation email has been sent to you by email.')
  55. return redirect(url_for('main.index'))
  56. return render_template('auth/register.html', form=form)
  57. @auth.route('/confirm/<token>')
  58. @login_required
  59. def confirm(token):
  60. if current_user.confirmed:
  61. return redirect(url_for('main.index'))
  62. if current_user.confirm(token):
  63. db.session.commit()
  64. flash('You have confirmed your account. Thanks!')
  65. else:
  66. flash('The confirmationlink is invalid or has expired')
  67. return redirect(url_for('main.index'))
  68. @auth.route('/confirm')
  69. @login_required
  70. def resend_confirmation():
  71. token = current_user.generate_confirmation_token()
  72. send_email(current_user.email, 'Confirm your account',
  73. 'auth/email/confirm', user=current_user, token=token)
  74. flash('A new confirmation email has been sent to you by email.')
  75. return redirect(url_for('main.index'))
  76. @auth.route('/change-password', methods=['GET', 'POST'])
  77. @login_required
  78. def change_password():
  79. form = ChangePasswordForm()
  80. if form.validate_on_submit():
  81. if current_user.verify_password(form.old_password.data):
  82. current_user.password = form.password.data
  83. db.session.add(current_user)
  84. db.session.commit()
  85. flash('Your password has been updated')
  86. redirect(url_for('main.index'))
  87. else:
  88. flash('Invalid password.')
  89. return render_template('auth/change_password.html', form=form)
  90. @auth.route('/reset', methods=['GET', 'POST'])
  91. def password_reset_request():
  92. if not current_user.is_anonymous:
  93. redirect(url_for('main.index'))
  94. form = PasswordResetRequestForm()
  95. if form.validate_on_submit():
  96. user = User.query.filter_by(email=form.email.data).first()
  97. if user:
  98. token = user.generate_reset_token()
  99. send_email(user.email, 'Reset your password',
  100. 'auth/email/reset_password', user=user, token=token)
  101. flash('An email with instructions to reset your password has been '
  102. 'sent to you')
  103. return redirect(url_for('auth.login'))
  104. return render_template('auth/reset_password.html', form=form)
  105. @auth.route('/reset/<token>', methods=['GET', 'POST'])
  106. def password_reset(token):
  107. if not current_user.is_anonymous:
  108. redirect(url_for('main.index'))
  109. form = PasswordResetForm()
  110. if form.validate_on_submit():
  111. if User.reset_password(token, form.password.data):
  112. db.session.commit()
  113. flash('Your password has been updated.')
  114. return redirect(url_for('auth.login'))
  115. else:
  116. return redirect(url_for('main.index'))
  117. return render_template('auth/reset_password.html', form=form)
  118. @auth.route('/change-email', methods=['GET', 'POST'])
  119. @login_required
  120. def change_email_request():
  121. form = ChangeEmailForm()
  122. if form.validate_on_submit():
  123. if current_user.verify_password(form.password.data):
  124. new_email = form.email.data
  125. token = current_user.generate_email_change_token(new_email)
  126. send_email(new_email, 'Confirm your email address',
  127. 'auth/email/change_email',
  128. user=current_user, token=token)
  129. flash('An email with instructions to confirm your new email '
  130. 'address has been sent to you')
  131. return redirect(url_for('main.index'))
  132. else:
  133. flash('Invalid email or password')
  134. return render_template('auth/change_email.html', form=form)
  135. @auth.route('/change-email/<token>')
  136. @login_required
  137. def change_email(token):
  138. if current_user.change_email(token):
  139. db.session.commit()
  140. flash('Your email address has been updated.')
  141. else:
  142. flash('Invalid request.')
  143. return redirect(url_for('main.index'))