From a0711939597f9c37549099b85833d7223743fc5f Mon Sep 17 00:00:00 2001 From: raphael Date: Thu, 23 Apr 2020 17:11:23 +0000 Subject: [PATCH] adds email login and registration forms - using flask-wtf forms to create login and registration - saves and compares input data with database - generates user if need-be - same user form as google user --- app.db | Bin 45056 -> 45056 bytes server/forms.py | 31 ++++++++++++++++++++++++++ server/models.py | 11 ++++++++-- server/routes.py | 36 +++++++++++++++++++++++++++++-- server/template/base.html | 17 +++++++++++++++ server/template/emaillogin.html | 19 ++++++++++++++++ server/template/login.html | 15 +++---------- server/template/register.html | 37 ++++++++++++++++++++++++++++++++ 8 files changed, 150 insertions(+), 16 deletions(-) create mode 100644 server/forms.py create mode 100644 server/template/base.html create mode 100644 server/template/emaillogin.html create mode 100644 server/template/register.html diff --git a/app.db b/app.db index 233c71c4b09daadb5035c4b51556ee2ab52e2b55..b6fb9828714fac150c0468ad7b44488677d25b67 100644 GIT binary patch delta 285 zcmZp8z|`=7X@WGP!bBNoMum+DOY%9G_}((`=kmSXEGY1hufC3nU0ht7q0!SLHLs`= zOk{vq4(YjxnK^pN`MCv2*(qs8R>c{KMy6&~hNcEUppu@S^5lPnD_4Gl~!jf^eK3@j4OEKQRW&CN}W z8IvaG)=%eP;=jwl|D69W&`lTl`8b)y8Nn`Ll3+IHoXnUn#?8RMz{G!zf&Uu+E1;TF z{3624+KgxpWt#kIz7)`?Ck*^g_}>AQ-Qbr&7$wN8$r+Sbkdc_0qu`sEnwOIb0DUr4 AuK)l5 delta 111 zcmZp8z|`=7X@WGP{6raNM){2iOY+$m`Ewcgb2keLB=b+st)C9$KWE^74&>kEXOUnw z=bX%#FUHBhz`)4=ih=(XQ2ahWBg5oZ^QC|)-!bsN1B*-yU='.format(self.username) + return ''.format(self.username) + + def setPassword(self, password): + self.password_hash = generate_password_hash(password) + + def checkPassword(self, password): + return check_password_hash(self.password_hash, password) class Calendar(db.Model): usr_id = db.Column(db.String(21), index=True) diff --git a/server/routes.py b/server/routes.py index fb238cf..ffb9d64 100644 --- a/server/routes.py +++ b/server/routes.py @@ -5,6 +5,7 @@ import sqlite3 # Third-party libraries import flask +from flask import render_template, flash from flask import Flask, redirect, request, url_for from flask_login import ( LoginManager, @@ -18,6 +19,7 @@ import requests import server.googleHandler as google from server import login_manager, app, db +from server.forms import LoginForm, RegistrationForm from server.models import User, Calendar @app.route("/") @@ -41,8 +43,39 @@ def calendar(): calendars = google.calendarsFromDb() return flask.render_template('calendar.html', calendars=calendars) +@app.route('/login/email', methods=['GET', 'POST']) +def emaillogin(): + if current_user.is_authenticated: + return redirect(url_for('account') ) + form = LoginForm() + + if form.validate_on_submit(): + user = User.query.filter_by(username=form.username.data).first() + if user is None or not user.checkPassword(form.password.data): + flash('invalid username or password') + return redirect(url_for('emaillogin')) + login_user(user, remember=form.remember_me.data) + return redirect(url_for('account')) + return render_template('emaillogin.html', title='Sign In', form=form) + +@app.route('/register', methods=['GET', 'POST']) +def register(): + if current_user.is_authenticated: + return redirect(url_for('account')) + form = RegistrationForm() + if form.validate_on_submit(): + user = User(id=form.username.data, + username=form.username.data, + email=form.email.data) + user.setPassword(form.password.data) + db.session.add(user) + db.session.commit() + flash('Congratulations, you are now a registered user!') + return redirect(url_for('emaillogin')) + return render_template('register.html', title='Register', form=form) + @app.route("/login/google") -def login(): +def googlelogin(): authorization_url = google.login() return flask.redirect(authorization_url) @@ -121,4 +154,3 @@ def user(): print("405") return 'OK' - diff --git a/server/template/base.html b/server/template/base.html new file mode 100644 index 0000000..f110dd4 --- /dev/null +++ b/server/template/base.html @@ -0,0 +1,17 @@ + + + + + + + + + Index + + + + + {% block content %}{% endblock %} + + + \ No newline at end of file diff --git a/server/template/emaillogin.html b/server/template/emaillogin.html new file mode 100644 index 0000000..7ee382a --- /dev/null +++ b/server/template/emaillogin.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block content %} +

Sign In

+
+ {{ form.hidden_tag() }} +

+ {{ form.username.label }}
+ {{ form.username(size=32) }} +

+

+ {{ form.password.label }}
+ {{ form.password(size=32) }} +

+

{{ form.remember_me() }} {{ form.remember_me.label }}

+

{{ form.submit() }}

+
+

New User? Click to Register!

+{% endblock %} \ No newline at end of file diff --git a/server/template/login.html b/server/template/login.html index 44c2f07..720fa2f 100644 --- a/server/template/login.html +++ b/server/template/login.html @@ -1,15 +1,7 @@ - - +{% extends "base.html" %} - - - - - - Index - +{% block content %} -

Login Page

@@ -41,6 +33,5 @@ - - +{% endblock %} \ No newline at end of file diff --git a/server/template/register.html b/server/template/register.html new file mode 100644 index 0000000..62e1b24 --- /dev/null +++ b/server/template/register.html @@ -0,0 +1,37 @@ +{% extends "base.html" %} + +{% block content %} +

Register

+
+ {{ form.hidden_tag() }} +

+ {{ form.username.label }}
+ {{ form.username(size=32) }}
+ {% for error in form.username.errors %} + [{{ error }}] + {% endfor %} +

+

+ {{ form.email.label }}
+ {{ form.email(size=64) }}
+ {% for error in form.email.errors %} + [{{ error }}] + {% endfor %} +

+

+ {{ form.password.label }}
+ {{ form.password(size=32) }}
+ {% for error in form.password.errors %} + [{{ error }}] + {% endfor %} +

+

+ {{ form.password2.label }}
+ {{ form.password2(size=32) }}
+ {% for error in form.password2.errors %} + [{{ error }}] + {% endfor %} +

+

{{ form.submit() }}

+
+{% endblock %} \ No newline at end of file