Code source de Planète Casio https://planet-casio.com
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.

43 lines
1.5KB

  1. from flask import redirect, url_for, request, flash
  2. from flask_login import login_user, logout_user, login_required, current_user
  3. from app import app
  4. from app.forms.login import LoginForm
  5. from app.models.users import Member
  6. from app.utils.render import render
  7. # from app.routes.index import index
  8. @app.route('/login', methods=['GET', 'POST'])
  9. def login():
  10. if current_user.is_authenticated:
  11. return redirect(url_for('index'))
  12. form = LoginForm()
  13. if form.validate_on_submit():
  14. member = Member.query.filter_by(name=form.username.data).first()
  15. if member is None or not member.check_password(form.password.data):
  16. flash('Pseudo ou mot de passe invalide', 'error')
  17. return redirect(request.referrer)
  18. login_user(member, remember=form.remember_me.data)
  19. # TODO: est-ce qu'on garde ce foutu message plus chiant qu'autre chose ?
  20. flash(f'Bon retour parmi nous, {current_user.name} !', 'info')
  21. if request.args.get('next'):
  22. return redirect(request.args.get('next'))
  23. if request.referrer:
  24. return redirect(request.referrer)
  25. return redirect(url_for('index'))
  26. return render('login.html', form=form)
  27. @app.route('/logout')
  28. @login_required
  29. def logout():
  30. try:
  31. print(request.referrer)
  32. except Exception as e:
  33. print('No referrer:', e)
  34. logout_user()
  35. flash('Déconnexion réussie', 'info')
  36. if request.referrer:
  37. return redirect(request.referrer)
  38. return redirect(url_for('index'))