Register account

This commit is contained in:
Emily 2019-04-26 04:20:31 +01:00
parent 538ce926f8
commit 04cb34bd92
4 changed files with 118 additions and 4 deletions

View File

@ -6,14 +6,39 @@ from objects import glob
class LoginForm(Form):
email = StringField("Email", [
validators.DataRequired(),
validators.Length(min=6, max=64)
validators.Length(min=6, max=254)
])
password = PasswordField("Password", [
validators.DataRequired(),
validators.Length(min=4)
validators.Length(min=4, max=127)
])
class RegisterForm(Form):
email = StringField("Email", [
validators.DataRequired(),
validators.Length(min=6, max=254)
])
password = PasswordField("Password", [
validators.DataRequired(),
validators.Length(min=4, max=127),
validators.EqualTo("confirm_password", message = "Passwords must match")
])
confirm_password = PasswordField("Repeat Password")
firstname = StringField("Firstname", [
validators.DataRequired(),
validators.Length(min=2, max=30)
])
surname = StringField("Surname", [
validators.DataRequired(),
validators.Length(min=2, max=30)
])
accept_tos = BooleanField("I accept the TOS", [validators.DataRequired()])
class User(UserMixin):
id = -1
email = ""
@ -43,3 +68,16 @@ class User(UserMixin):
raise Exception("Invalid login")
self.id, self.email, self.password, self.firstname, self.surname = user
def register_account(email, password, firstname, surname):
conn = glob.get_sql_connection()
cur = conn.cursor()
cur.execute("""
INSERT INTO
Bruker (Epost, Passord, Fornavn, Etternavn)
VALUES (%s, %s, %s, %s);
""", (email, password, firstname, surname))
conn.commit()
cur.close()

View File

@ -2,7 +2,7 @@ from flask import render_template, url_for, request, redirect, flash, abort
from wtforms import Form, BooleanField, StringField, PasswordField, validators
import flask_login
from forms.login import LoginForm, User
from forms.login import LoginForm, RegisterForm, User, register_account
from objects import glob # Global sharing of python objects in a manageable way
@ -40,6 +40,24 @@ def login():
return redirect(url_for("dashboard"))
return render_template("login.html", form=form)
@glob.app.route("/register", methods = ["GET", "POST"])
def register():
if flask_login.current_user.is_authenticated:
flash("Already logged in", "info")
return redirect(url_for("dashboard"))
form = RegisterForm(request.form)
if request.method == "POST" and form.validate():
try:
register_account(form.email.data, form.password.data, form.firstname.data, form.surname.data)
except Exception as e:
flash(str(e), "danger")
return render_template("register.html", form=form)
flash("User registered", "success")
return redirect(url_for("login"))
return render_template("register.html", form=form)
@glob.app.route("/logout")
@flask_login.login_required
def logout():

View File

@ -8,8 +8,11 @@
<div class="row alert-section">
<div class="col-md-8 mx-auto">
<div class="card rounded-1">
<div class="card-header">
<div class="card-header navbar topnav">
<h3 class="mb-0">Login Methods</h3>
<div class="my-2 my-lg-0 d-flex">
<a href="{{ url_for('register') }}">Register</a>
</div>
</div>
<div class="card-body row">
<div class="col-md-6">

55
templates/register.html Normal file
View File

@ -0,0 +1,55 @@
{% set title = "Register" %}
{% extends "layout/bootstrap.html" %}
{% block content %}
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
<script src="{{ url_for('static', filename='js/alerts.js') }}"></script>
<div class="container pt-3">
<div class="row alert-section">
<div class="col-md-8 mx-auto">
<div class="card rounded-1">
<div class="card-header">
<h3 class="mb-0">Register</h3>
</div>
<div class="card-body row">
<div class="col-md-6">
<form method=post>
<dl>
{{ render_field(form.email) }}
{{ render_field(form.password) }}
{{ render_field(form.confirm_password) }}
{{ render_field(form.firstname) }}
{{ render_field(form.surname) }}
{{ render_field(form.accept_tos) }}
</dl>
<input type=submit value="Register">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
{% with messages = get_flashed_messages() %}
{% if messages %}
let alertArea = document.getElementsByClassName("alert-section")[0];
{% for message in messages %}
alertAbove(alertArea, "info", "{{ message }}");
{% endfor %}
{% endif %}
{% endwith %}
</script>
{% endblock %}