diff --git a/app.db b/app.db index 233c71c..b6fb982 100644 Binary files a/app.db and b/app.db differ diff --git a/server/forms.py b/server/forms.py new file mode 100644 index 0000000..311ec75 --- /dev/null +++ b/server/forms.py @@ -0,0 +1,31 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, PasswordField, BooleanField, SubmitField +from wtforms.validators import DataRequired, ValidationError, Email, EqualTo +from server.models import User +import email_validator + +class LoginForm(FlaskForm): + username = StringField('Username', validators=[DataRequired()]) + password = PasswordField('Password', validators=[DataRequired()]) + remember_me = BooleanField('Remember Me') + submit = SubmitField('Sign In') + + + +class RegistrationForm(FlaskForm): + username = StringField('Username', validators=[DataRequired()]) + email = StringField('Email', validators=[DataRequired(), Email()]) + password = PasswordField('Password', validators=[DataRequired()]) + password2 = PasswordField( + 'Repeat Password', validators=[DataRequired(), EqualTo('password')]) + submit = SubmitField('Register') + + def validate_username(self, username): + user = User.query.filter_by(username=username.data).first() + if user is not None: + raise ValidationError('Please use a different username.') + + def validate_email(self, email): + user = User.query.filter_by(email=email.data).first() + if user is not None: + raise ValidationError('Please use a different email address.') \ No newline at end of file diff --git a/server/models.py b/server/models.py index de8473d..58cd8ce 100644 --- a/server/models.py +++ b/server/models.py @@ -1,19 +1,26 @@ from flask_login import UserMixin from server import login_manager, db +from werkzeug.security import generate_password_hash, check_password_hash @login_manager.user_loader def load_user(id): return User.query.get(id) class User(UserMixin, db.Model): - id = db.Column(db.String(21), primary_key=True) + id = db.Column(db.String(64), primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) profile_pic = db.Column(db.String(256)) password_hash = db.Column(db.String(128)) def __repr__(self): - return ''.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