From 3fe56893b6413cf04c476978fb235a5e3514c7c3 Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Sun, 12 Feb 2023 11:08:57 +0000 Subject: [PATCH] Simple login and registration in Flask with MongoDB --- app.py | 107 ++++++++++++++++++++++++++- src/events/school_time/email_read.py | 1 - templates/base.html | 35 +++++++++ templates/index.html | 49 +++++++++--- templates/logged_in.html | 7 ++ templates/login.html | 24 ++++++ templates/signout.html | 7 ++ templates/signup.html | 54 ++++++++++++++ 8 files changed, 269 insertions(+), 15 deletions(-) create mode 100644 templates/base.html create mode 100644 templates/logged_in.html create mode 100644 templates/login.html create mode 100644 templates/signout.html create mode 100644 templates/signup.html diff --git a/app.py b/app.py index 04a87e3..8c583b0 100644 --- a/app.py +++ b/app.py @@ -1,10 +1,16 @@ from urllib.request import Request, urlopen -from flask import Flask, render_template +from flask import Flask, render_template, url_for, request, redirect, session, flash +from flask_pymongo import PyMongo +from dotenv import load_dotenv +import bcrypt +import urllib +from pymongo import MongoClient import logging import json import sys import os +load_dotenv() logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", @@ -15,8 +21,9 @@ logging.basicConfig( ) app = Flask(__name__) -IMAGE_FOLDER = os.path.join('static', 'images') -app.config['UPLOAD_FOLDER'] = IMAGE_FOLDER +app.secret_key = "testing" +PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB +URL_MONGODB = os.getenv('URL_MONGODB') #URL for MongoDB @app.route('/') def homepage(): @@ -37,6 +44,100 @@ def orario(): day = str(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]) return render_template('orario/orario.html', data=dict, number=number, day=day) +# #connect to your Mongo DB database +def MongoDB(): + client = MongoClient("mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB) + db = client.get_database('website-class') + records = db.users + return records + +records = MongoDB() + + +#assign URLs to have a particular route +@app.route("/register", methods=['post', 'get']) +def register(): + message = '' + #if method post in index + if "email" in session: + return redirect(url_for("logged_in")) + if request.method == "POST": + user = request.form.get("fullname") + email = request.form.get("email") + password1 = request.form.get("password1") + password2 = request.form.get("password2") + #if found in database showcase that it's found + user_found = records.find_one({"name": user}) + email_found = records.find_one({"email": email}) + if user_found: + message = 'There already is a user by that name' + return render_template('index.html', message=message) + if email_found: + message = 'This email already exists in database' + return render_template('index.html', message=message) + if password1 != password2: + message = 'Passwords should match!' + return render_template('index.html', message=message) + else: + #hash the password and encode it + hashed = bcrypt.hashpw(password2.encode('utf-8'), bcrypt.gensalt()) + #assing them in a dictionary in key value pairs + user_input = {'name': user, 'email': email, 'password': hashed} + #insert it in the record collection + records.insert_one(user_input) + + #find the new created account and its email + user_data = records.find_one({"email": email}) + new_email = user_data['email'] + #if registered redirect to logged in as the registered user + return render_template('logged_in.html', email=new_email) + return render_template('index.html') + +@app.route("/login", methods=["POST", "GET"]) +def login(): + message = 'Please login to your account' + if "email" in session: + return redirect(url_for("logged_in")) + + if request.method == "POST": + email = request.form.get("email") + password = request.form.get("password") + + #check if email exists in database + email_found = records.find_one({"email": email}) + if email_found: + email_val = email_found['email'] + passwordcheck = email_found['password'] + #encode the password and check if it matches + if bcrypt.checkpw(password.encode('utf-8'), passwordcheck): + session["email"] = email_val + return redirect(url_for('logged_in')) + else: + if "email" in session: + return redirect(url_for("logged_in")) + message = 'Wrong password' + return render_template('login.html', message=message) + else: + message = 'Email not found' + return render_template('login.html', message=message) + return render_template('login.html', message=message) + +@app.route('/logged_in') +def logged_in(): + if "email" in session: + email = session["email"] + return render_template('logged_in.html', email=email) + else: + return redirect(url_for("login")) + +@app.route("/logout", methods=["POST", "GET"]) +def logout(): + if "email" in session: + session.pop("email", None) + return render_template("signout.html") + else: + return render_template('index.html') + if __name__ == '__main__': logging.info("Web server started!") app.run(port=4999, debug=True) \ No newline at end of file diff --git a/src/events/school_time/email_read.py b/src/events/school_time/email_read.py index 0a283bd..d02e08b 100644 --- a/src/events/school_time/email_read.py +++ b/src/events/school_time/email_read.py @@ -71,5 +71,4 @@ def send_xlsx(): update_time_school() os.remove(f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Delete file recheck_email() - check_email() \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..9afa31b --- /dev/null +++ b/templates/base.html @@ -0,0 +1,35 @@ + + + + + + {% block title %}{%endblock %} + + + + + + + + + + + + + + +{% block content %}{% endblock %} + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index f818a71..7b0f72f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,13 +1,40 @@ - - - - - 2 Elci - Homepage - - - +{% extends "base.html" %} +{% block title %}Register System{% endblock %} -

Hello World!

+{% block content %} - - \ No newline at end of file +{% if message %} + +{% endif %} + +
+
+ + + Please enter your full name(First name and Last name) +
+
+ + + We'll never share your email with anyone else. +
+
+ + +
+ +
+ + + +
+ + + +
+ + + +{% endblock %} \ No newline at end of file diff --git a/templates/logged_in.html b/templates/logged_in.html new file mode 100644 index 0000000..2c8d729 --- /dev/null +++ b/templates/logged_in.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block title %}Youv'e logged in {% endblock %} + +{% block content %} +

Hello You have Logged in as {{ email }}

+ +{% endblock %} \ No newline at end of file diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..f1a0eab --- /dev/null +++ b/templates/login.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% block title %}Login System{% endblock %} + +{% block content %} + +{% if message %} + +{% endif %} + +
+
+ + + We'll never share your email with anyone else. +
+
+ + +
+ +
+{% endblock %} \ No newline at end of file diff --git a/templates/signout.html b/templates/signout.html new file mode 100644 index 0000000..14d391c --- /dev/null +++ b/templates/signout.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block title %}Register System{% endblock %} + +{% block content %} +

You are signed out!

+ +{% endblock %} \ No newline at end of file diff --git a/templates/signup.html b/templates/signup.html new file mode 100644 index 0000000..9f8e129 --- /dev/null +++ b/templates/signup.html @@ -0,0 +1,54 @@ + + + + Login with mongodb flask + + + + + + + + + +
+
+ +

Login with mongodb flask

+
+ +
+

Signup

+ {% with messages = get_flashed_messages() %} + {% if messages %} +
    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor %} +
+ {% endif %} + {% endwith %} + +
+ + + +
+ + \ No newline at end of file