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.

преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # @name: auth.py
  2. # @creation_date: 2021-10-20
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: auth route for authorisation functions like logging in, signing up, and logging out
  6. # @acknowledgements:
  7. # https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
  8. from flask import Blueprint, render_template, redirect, url_for, request, flash
  9. from werkzeug.security import generate_password_hash, check_password_hash
  10. from flask_login import login_user, logout_user, login_required
  11. from .models import User
  12. from . import db
  13. auth = Blueprint('auth', __name__)
  14. # routes for login page
  15. @auth.route('/login')
  16. def login():
  17. return render_template('login.html')
  18. @auth.route('/login', methods=['POST'])
  19. def login_post():
  20. email = request.form.get('email')
  21. password = request.form.get('password')
  22. remember = True if request.form.get('remember') else False
  23. # SQLAlchemy query to get user data based on their email
  24. user = User.query.filter_by(email=email).first()
  25. # check if the user actually exists
  26. # take the user-supplied password, hash it, and compare it to the hashed password in the database
  27. if not user or not check_password_hash(user.password, password):
  28. flash('Please check your login details and try again.')
  29. return redirect(url_for('auth.login')) # if the user doesn't exist or password is wrong, reload the page
  30. # if the above check passes, then we know the user has the right credentials
  31. login_user(user, remember=remember)
  32. return redirect(url_for('main.profile'))
  33. # routes for signup page
  34. @auth.route('/signup')
  35. def signup():
  36. return render_template('signup.html')
  37. @auth.route('/signup', methods=['POST'])
  38. def signup_post():
  39. email = request.form.get('email')
  40. name = request.form.get('name')
  41. password = request.form.get('password')
  42. user = User.query.filter_by(email=email).first() # if this returns a user, then the email already exists in database
  43. if user: # if a user is found, we want to redirect back to signup page so user can try again
  44. flash('Email address already exists')
  45. return redirect(url_for('auth.signup'))
  46. # create a new user with the form data. Hash the password so the plaintext version isn't saved.
  47. new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'))
  48. # add the new user to the database
  49. db.session.add(new_user)
  50. db.session.commit()
  51. return redirect(url_for('auth.login'))
  52. # route for logout function
  53. @auth.route('/logout')
  54. @login_required
  55. def logout():
  56. logout_user()
  57. return redirect(url_for('main.index'))