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.

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