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.

102 lines
3.4 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. @auth.before_app_request
  9. def before_request():
  10. if current_user.is_authenticated \
  11. and not current_user.confirmed \
  12. and request.blueprint != 'auth' \
  13. and request.endpoint != 'static':
  14. return redirect(url_for('auth.unconfirmed'))
  15. @auth.route('/unconfirmed')
  16. def unconfirmed():
  17. if current_user.is_anonymous or current_user.confirmed:
  18. return redirect(url_for('main.index'))
  19. return render_template('auth/unconfirmed.html')
  20. @auth.route('/login', methods=['GET', 'POST'])
  21. def login():
  22. form = LoginForm()
  23. if form.validate_on_submit():
  24. user = User.query.filter_by(email=form.email.data).first()
  25. if user is not None and user.verify_password(form.password.data):
  26. login_user(user, form.remember_me.data)
  27. next = request.args.get('next')
  28. if next is None or not next.startswith('/'):
  29. next = url_for('main.index')
  30. return redirect(next)
  31. flash('Invalid username or password')
  32. return render_template('auth/login.html', form=form)
  33. @auth.route('/logout')
  34. @login_required
  35. def logout():
  36. logout_user()
  37. flash('You have been logged out.')
  38. return redirect(url_for('main.index'))
  39. @auth.route('/register', methods=['GET', 'POST'])
  40. def register():
  41. form = RegistrationForm()
  42. if form.validate_on_submit():
  43. user = User(email=form.email.data,
  44. username=form.username.data,
  45. password=form.password.data)
  46. db.session.add(user)
  47. db.session.commit()
  48. token = user.generate_confirmation_token()
  49. send_email(user.email, 'Confirm your account',
  50. 'auth/email/confirm', user=user, token=token)
  51. flash('A confirmation email has been sent to you by email.')
  52. return redirect(url_for('main.index'))
  53. return render_template('auth/register.html', form=form)
  54. @auth.route('/confirm/<token>')
  55. @login_required
  56. def confirm(token):
  57. if current_user.confirmed:
  58. return redirect(url_for('main.index'))
  59. if current_user.confirm(token):
  60. db.session.commit()
  61. flash('You have confirmed your account. Thanks!')
  62. else:
  63. flash('The confirmationlink is invalid or has expired')
  64. return redirect(url_for('main.index'))
  65. @auth.route('/confirm')
  66. @login_required
  67. def resend_confirmation():
  68. token = current_user.generate_confirmation_token()
  69. send_email(current_user.email, 'Confirm your account',
  70. 'auth/email/confirm', user=current_user, token=token)
  71. flash('A new confirmation email has been sent to you by email.')
  72. return redirect(url_for('main.index'))
  73. @auth.route('/change-password', methods=['GET', 'POST'])
  74. @login_required
  75. def change_password():
  76. form = ChangePasswordForm()
  77. if form.validate_on_submit():
  78. if current_user.verify_password(form.old_password.data):
  79. current_user.password = form.password.data
  80. db.session.add(current_user)
  81. db.session.commit()
  82. flash('Your password has been updated')
  83. redirect(url_for('main.index'))
  84. else:
  85. flash('Invalid password.')
  86. return render_template('auth/change_password.html', form=form)