2020-09-20 09:53:30 +01:00
|
|
|
from os import kill
|
2020-08-27 06:50:08 +01:00
|
|
|
from flask import request, render_template, session, redirect, url_for, flash
|
|
|
|
from flask_login import login_user, logout_user, current_user
|
2020-09-20 09:53:30 +01:00
|
|
|
from secrets import token_urlsafe
|
2020-08-27 09:06:38 +01:00
|
|
|
from wowstash.blueprints.auth import auth_bp
|
2020-08-27 06:50:08 +01:00
|
|
|
from wowstash.forms import Register, Login
|
2020-08-25 20:48:50 +01:00
|
|
|
from wowstash.models import User
|
2020-08-27 06:50:08 +01:00
|
|
|
from wowstash.factory import db, bcrypt
|
2020-09-21 08:03:04 +01:00
|
|
|
from wowstash.library.docker import docker
|
2020-08-21 08:39:59 +01:00
|
|
|
|
|
|
|
|
2020-08-27 09:06:38 +01:00
|
|
|
@auth_bp.route("/register", methods=["GET", "POST"])
|
2020-08-25 20:48:50 +01:00
|
|
|
def register():
|
|
|
|
form = Register()
|
2020-08-27 06:50:08 +01:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
flash('Already registered and authenticated.')
|
|
|
|
return redirect(url_for('wallet.dashboard'))
|
|
|
|
|
2020-08-25 20:48:50 +01:00
|
|
|
if form.validate_on_submit():
|
2020-08-27 06:50:08 +01:00
|
|
|
# Check if email already exists
|
|
|
|
user = User.query.filter_by(email=form.email.data).first()
|
|
|
|
if user:
|
|
|
|
flash('This email is already registered.')
|
2020-08-27 09:06:38 +01:00
|
|
|
return redirect(url_for('auth.login'))
|
2020-08-27 06:50:08 +01:00
|
|
|
|
|
|
|
# Save new user
|
|
|
|
user = User(
|
|
|
|
email=form.email.data,
|
|
|
|
password=bcrypt.generate_password_hash(form.password.data).decode('utf8'),
|
2020-09-20 09:53:30 +01:00
|
|
|
wallet_password=token_urlsafe(16),
|
2020-08-27 06:50:08 +01:00
|
|
|
)
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# Login user and redirect to wallet page
|
|
|
|
login_user(user)
|
|
|
|
return redirect(url_for('wallet.dashboard'))
|
|
|
|
|
2020-08-27 09:06:38 +01:00
|
|
|
return render_template("auth/register.html", form=form)
|
2020-08-21 08:39:59 +01:00
|
|
|
|
2020-08-27 09:06:38 +01:00
|
|
|
@auth_bp.route("/login", methods=["GET", "POST"])
|
2020-08-25 20:48:50 +01:00
|
|
|
def login():
|
2020-08-27 06:50:08 +01:00
|
|
|
form = Login()
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
flash('Already registered and authenticated.')
|
|
|
|
return redirect(url_for('wallet.dashboard'))
|
2020-08-27 09:06:38 +01:00
|
|
|
|
2020-08-27 06:50:08 +01:00
|
|
|
if form.validate_on_submit():
|
|
|
|
# Check if user doesn't exist
|
|
|
|
user = User.query.filter_by(email=form.email.data).first()
|
|
|
|
if not user:
|
|
|
|
flash('Invalid username or password.')
|
2020-08-27 09:06:38 +01:00
|
|
|
return redirect(url_for('auth.login'))
|
2020-08-27 06:50:08 +01:00
|
|
|
|
|
|
|
# Check if password is correct
|
|
|
|
password_matches = bcrypt.check_password_hash(
|
|
|
|
user.password,
|
|
|
|
form.password.data
|
|
|
|
)
|
|
|
|
if not password_matches:
|
|
|
|
flash('Invalid username or password.')
|
2020-08-27 09:06:38 +01:00
|
|
|
return redirect(url_for('auth.login'))
|
2020-08-27 06:50:08 +01:00
|
|
|
|
|
|
|
# Login user and redirect to wallet page
|
|
|
|
login_user(user)
|
|
|
|
return redirect(url_for('wallet.dashboard'))
|
|
|
|
|
2020-08-27 09:06:38 +01:00
|
|
|
return render_template("auth/login.html", form=form)
|
2020-08-21 08:39:59 +01:00
|
|
|
|
2020-08-27 09:06:38 +01:00
|
|
|
@auth_bp.route("/logout")
|
2020-08-21 08:39:59 +01:00
|
|
|
def logout():
|
2020-09-20 09:53:30 +01:00
|
|
|
if current_user.is_authenticated:
|
2020-09-21 08:03:04 +01:00
|
|
|
docker.stop_container(current_user.wallet_container)
|
2020-09-20 09:53:30 +01:00
|
|
|
current_user.clear_wallet_data()
|
2020-08-27 06:50:08 +01:00
|
|
|
logout_user()
|
|
|
|
return redirect(url_for('meta.index'))
|