mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-05 16:38:59 -04:00
108536490: Initial effort to implement log in
Add endpoint for post to /sign-in Initialise role data
This commit is contained in:
@@ -3,4 +3,4 @@ from flask import Blueprint
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
|
||||
from app.main.views import index
|
||||
from app.main.views import index, sign_in
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from app import db
|
||||
from app.models import Users
|
||||
from app.main.encryption import encrypt
|
||||
|
||||
|
||||
def insert_user(user):
|
||||
user.password = encrypt(user.password)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
@@ -13,3 +15,7 @@ def get_user_by_id(id):
|
||||
|
||||
def get_all_users():
|
||||
return Users.query.all()
|
||||
|
||||
|
||||
def get_user_by_email(email_address):
|
||||
return Users.query.filter_by(email_address=email_address).first()
|
||||
|
||||
7
app/main/encryption.py
Normal file
7
app/main/encryption.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import hashlib
|
||||
from flask import current_app
|
||||
|
||||
|
||||
def encrypt(value):
|
||||
key = current_app.config['SECRET_KEY']
|
||||
return hashlib.sha256((key + value).encode('UTF-8')).hexdigest()
|
||||
14
app/main/forms.py
Normal file
14
app/main/forms.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from flask_wtf import Form
|
||||
from wtforms import StringField, PasswordField
|
||||
from wtforms.validators import DataRequired, Email, Length
|
||||
|
||||
|
||||
class LoginForm(Form):
|
||||
email_address = StringField('Email address', validators=[
|
||||
Length(255),
|
||||
DataRequired(message='Email cannot be empty'),
|
||||
Email(message='Please enter a valid email address')
|
||||
])
|
||||
password = PasswordField('Password', validators=[
|
||||
DataRequired(message='Please enter your password')
|
||||
])
|
||||
@@ -43,11 +43,6 @@ def dashboard():
|
||||
return render_template('dashboard.html')
|
||||
|
||||
|
||||
@main.route("/sign-in")
|
||||
def signin():
|
||||
return render_template('signin.html')
|
||||
|
||||
|
||||
@main.route("/add-service")
|
||||
def addservice():
|
||||
return render_template('add-service.html')
|
||||
|
||||
43
app/main/views/sign_in.py
Normal file
43
app/main/views/sign_in.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from datetime import datetime
|
||||
|
||||
from flask import render_template, redirect, url_for, jsonify
|
||||
from flask_login import login_user
|
||||
|
||||
from app.main import main
|
||||
from app.main.forms import LoginForm
|
||||
from app.main.dao import users_dao
|
||||
from app.models import Users
|
||||
from app.main.encryption import encrypt
|
||||
|
||||
|
||||
@main.route("/sign-in", methods=(['GET']))
|
||||
def render_sign_in():
|
||||
return render_template('signin.html', form=LoginForm())
|
||||
|
||||
|
||||
@main.route('/sign-in', methods=(['POST']))
|
||||
def process_sign_in():
|
||||
form = LoginForm()
|
||||
if form.validate_on_submit():
|
||||
user = users_dao.get_user_by_email(form.email_address)
|
||||
if user is None:
|
||||
return jsonify(authorization=False), 404
|
||||
if user.password == encrypt(form.password):
|
||||
login_user(user)
|
||||
else:
|
||||
return jsonify(authorization=False), 404
|
||||
|
||||
return redirect('/two-factor')
|
||||
|
||||
|
||||
@main.route('/create_user', methods=(['POST']))
|
||||
def create_user_for_test():
|
||||
form = LoginForm()
|
||||
user = Users(email_address=form.email_address,
|
||||
name=form.email_address,
|
||||
password=form.password,
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
users_dao.insert_user(user)
|
||||
|
||||
return 'created'
|
||||
@@ -1,7 +1,7 @@
|
||||
{% extends "admin_template.html" %}
|
||||
|
||||
{% block page_title %}
|
||||
Hello world!
|
||||
Sign in
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
@@ -12,19 +12,24 @@ Hello world!
|
||||
|
||||
<p>If you do not have an account, you can <a href="register">register</a>.</p>
|
||||
|
||||
<p>
|
||||
<label class="form-label" for="email">Email address</label>
|
||||
<input class="form-control-2-3" id="email" type="text"><br>
|
||||
</p>
|
||||
<p>
|
||||
<label class="form-label" for="password">Password</label>
|
||||
<input class="form-control-1-4" id="password" type="password"><br>
|
||||
<span class="font-xsmall"><a href="forgot-password">Forgotten password?</a></span>
|
||||
</p>
|
||||
<form autocomplete="off" action="" method="post">
|
||||
|
||||
<p>
|
||||
<a class="button" href="two-factor" role="button">Continue</a>
|
||||
</p>
|
||||
<p>
|
||||
<label class="form-label">Email address</label>
|
||||
{{ form.email_address(class="form-control-2-3", autocomplete="off") }} <br>
|
||||
</p>
|
||||
<p>
|
||||
<label class="form-label">Password</label>
|
||||
{{ form.password(class="form-control-1-4", autocomplete="off") }} <br>
|
||||
</p>
|
||||
<p>
|
||||
<span class="font-xsmall"><a href="">Forgotten password?</a></span>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<a class="button" href="two-factor" role="button">Continue</a>
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user