Skip to content
Snippets Groups Projects
Commit 2e68e0b4 authored by Teemu Autto's avatar Teemu Autto
Browse files

Base template and user registering.

parent bddb1ee2
No related branches found
No related tags found
1 merge request!20Merge of the changes done in propaedeutic.
......@@ -33,6 +33,7 @@ def create_app(config: Optional[Dict] = None) -> Flask:
flask_app.config.from_mapping(
SECRET_KEY='dev',
BRAND="Hill Valley DMC dealership",
)
# load the instance config, if it exists, when not testing
......@@ -55,6 +56,9 @@ def create_app(config: Optional[Dict] = None) -> Flask:
def hello():
return 'Hello, World!'
from . import auth
flask_app.register_blueprint(auth.bp)
# Register blueprints
from . import views # pylint: disable=import-outside-toplevel
flask_app.register_blueprint(views.bp, url_prefix='')
......
import functools
from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from werkzeug.security import check_password_hash, generate_password_hash
from .models import User
from mongoengine import errors
bp = Blueprint('auth', __name__, url_prefix='/auth')
@bp.route('/register', methods=('GET', 'POST'))
def register():
if request.method == 'POST':
print("Registering user...")
email = request.form['email']
password = request.form['password']
password2 = request.form['password2']
terms = request.form.get('terms', False)
error = None
if not email:
error = 'Username is required.'
elif not password:
error = 'Password is required.'
elif password != password2:
error = 'Passwords do not match.'
elif not terms:
error = 'You must agree to the terms.'
if error is None:
try:
user = User(
email=email,
password=generate_password_hash(password)
)
user.save()
except Exception as exc:
error = f"Error creating user: {exc!s}"
else:
return redirect(url_for("auth.login"))
print("Could not register user:", error)
flash(error)
return render_template('auth/register.html')
@bp.route('/login', methods=('GET', 'POST'))
def login():
...
@bp.route('/logout')
def logout():
...
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Register{% endblock %}</h1>
{% endblock %}
{% block content %}
<section class="min-vh-100 d-flex align-items-center bg-secondary">
<div class="container">
<div class="row justify-content-center">
<div class="col-12 d-flex align-items-center justify-content-center">
<div class="card card-tertiary w-100 fmxw-400">
<div class="card-header text-center">
<span>Register on our platform</span>
</div>
<div class="card-body">
<form action="{{ url_for('auth.register') }}" method="POST" class="mt-4">
<div class="form-group">
<label for="email" class="mb-2">Email</label>
<input name="email" id="email" type="email" class="form-control" placeholder="Your email" required="">
</div>
<div class="form-group">
<div class="form-group">
<label for="password" class="mb-2">Password</label>
<input name="password" id="password" type="password" class="form-control" placeholder="Your password" required="">
</div>
<div class="form-group">
<label for="confirmPassword" class="mb-2">Confirm password</label>
<input name="password2" id="confirmPassword" type="password" class="form-control" placeholder="Confirm password"
required="">
</div>
<div class="d-flex justify-content-between align-items-center mb-4">
<div class="form-check">
<label class="form-check-label">
<input name="terms" class="form-check-input" type="checkbox">
<span class="form-check-x"></span>
<span class="form-check-sign"></span>
I agree to the <a href="#">terms and conditions</a>
</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-block btn-primary">Register account</button>
</form>
<div class="d-block d-sm-flex justify-content-center align-items-center mt-4">
<p class="font-weight-normal">
Already have an account?
<a href="./login.html" class="font-weight-bold">Login here</a>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
{% endblock %}
<!doctype html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %} - {{ config['BRAND'] }}</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<link rel="stylesheet" href="{{ url_for('static', filename='windows-95-ui-kit/css/w95.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarTogglerDemo03">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<ul>
{% if g.user %}
<li class="nav-item">
<a class="nav-link" href="#"><span>{{ g.user['username'] }}</span></a>
<a class="nav-link" href="#"><a href="{{ url_for('auth.logout') }}">Log Out</a></a>
</li>
{% else %}
<li class="nav-item">
<a href="{{ url_for('auth.register') }}">Register</a>
</li>
<li class="nav-item">
<a href="{{ url_for('auth.login') }}">Log In</a>
</li>
{% endif %}
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<header>
{% block header %}
<h1>{{ config['BRAND'] }}</h1>
{% endblock %}
</header>
{% for message in get_flashed_messages() %}
<div class="flash alert alert-primary">{{ message }}</div>
{% endfor %}
{% block content %}
<!-- MAIN CONTENT BLOCK MISSING -->
{% endblock %}
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment