Prototype functionality for login, logout, and sign up.
This commit is contained in:
parent
6869ff0cc1
commit
2e41f7da53
9 changed files with 151 additions and 42 deletions
3
uwume/assets/signup/content.txt
Normal file
3
uwume/assets/signup/content.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
This is a personal project meant to allow users to sign up and receive an "UwU" from guests and other users!
|
||||||
|
|
||||||
|
Credit for the original concept for this was PleasePet.
|
|
@ -5,7 +5,8 @@ from flask_login import LoginManager, UserMixin, login_required
|
||||||
class User(UserMixin):
|
class User(UserMixin):
|
||||||
# proxy for a database of users
|
# proxy for a database of users
|
||||||
user_database = {"JohnDoe": ("JohnDoe", "John"),
|
user_database = {"JohnDoe": ("JohnDoe", "John"),
|
||||||
"JaneDoe": ("JaneDoe", "Jane")}
|
"JaneDoe": ("JaneDoe", "Jane"),
|
||||||
|
"admin": ("admin", "admin")}
|
||||||
|
|
||||||
def __init__(self, username, password):
|
def __init__(self, username, password):
|
||||||
self.id = username
|
self.id = username
|
||||||
|
|
0
uwume/static/css/signup/index.css
Normal file
0
uwume/static/css/signup/index.css
Normal file
0
uwume/static/js/signup/index.js
Normal file
0
uwume/static/js/signup/index.js
Normal file
|
@ -3,7 +3,7 @@
|
||||||
UwU Me!
|
UwU Me!
|
||||||
</h1>
|
</h1>
|
||||||
<h5 class="grey-text text-lighten-1 center">
|
<h5 class="grey-text text-lighten-1 center">
|
||||||
Hello, {{ user }}
|
Hello, {{ current_user.id }}
|
||||||
</h5>
|
</h5>
|
||||||
{% endblock %} {% block content %}
|
{% endblock %} {% block content %}
|
||||||
<div class="card-panel grey darken-3">
|
<div class="card-panel grey darken-3">
|
||||||
|
@ -13,4 +13,11 @@
|
||||||
</p>
|
</p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
<div class="card-panel grey darken-3">
|
||||||
|
<div class="center">
|
||||||
|
<a class="waves-effect waves-light btn blue darken-2" href="/logout">
|
||||||
|
Logout
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,22 +1,29 @@
|
||||||
{% extends 'index.html' %}
|
{% extends 'index.html' %} {% block header %}
|
||||||
|
|
||||||
{% block header %}
|
|
||||||
<h1 class="blue-grey-text text-lighten-5 center">
|
<h1 class="blue-grey-text text-lighten-5 center">
|
||||||
UwU Me!
|
UwU Me!
|
||||||
</h1>
|
</h1>
|
||||||
{% endblock %}
|
{% endblock %} {% block content %}
|
||||||
|
<div class="card-panel grey darken-3">
|
||||||
{% block content %}
|
<div class="row">
|
||||||
<div class="card-panel grey darken-3">
|
<div class="col s12">
|
||||||
<div class="center">
|
<div class="row">
|
||||||
<a class="waves-effect waves-light btn blue darken-2" href="/login">
|
<div class="center col s6">
|
||||||
Login
|
<a class="waves-effect waves-light btn blue darken-2" href="/login">
|
||||||
</a>
|
Login
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% for item in content_text %}
|
<div class="center col s6">
|
||||||
<p class="blue-grey-text text-lighten-5">
|
<a class="waves-effect waves-light btn blue darken-2" href="/signup">
|
||||||
{{ item }}
|
Sign Up
|
||||||
</p>
|
</a>
|
||||||
{% endfor %}
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
{% for item in content_text %}
|
||||||
|
<p class="blue-grey-text text-lighten-5">
|
||||||
|
{{ item }}
|
||||||
|
</p>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -17,6 +17,7 @@
|
||||||
name="username"
|
name="username"
|
||||||
value="{{
|
value="{{
|
||||||
request.form.username }}"
|
request.form.username }}"
|
||||||
|
id="username"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="input-field col s6">
|
<div class="input-field col s6">
|
||||||
|
@ -26,6 +27,7 @@
|
||||||
name="password"
|
name="password"
|
||||||
value="{{
|
value="{{
|
||||||
request.form.password }}"
|
request.form.password }}"
|
||||||
|
id="password"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
59
uwume/templates/signup/index.html
Normal file
59
uwume/templates/signup/index.html
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
{% extends 'index.html' %} {% block header %}
|
||||||
|
<h1 class="blue-grey-text text-lighten-5 center">
|
||||||
|
UwU Me!
|
||||||
|
</h1>
|
||||||
|
<h5 class="grey-text text-lighten-1 center">
|
||||||
|
Sign Up Page
|
||||||
|
</h5>
|
||||||
|
{% endblock %} {% block content %}
|
||||||
|
{% for message in get_flashed_messages() %}
|
||||||
|
<div class="card-panel red accent-1">
|
||||||
|
<p>
|
||||||
|
{{ message }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
<div class="card-panel grey darken-3">
|
||||||
|
<div class="row">
|
||||||
|
<form class="col s12" action="" method="post">
|
||||||
|
<div class="row">
|
||||||
|
<div class="input-field col s6">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Username"
|
||||||
|
name="username"
|
||||||
|
value="{{
|
||||||
|
request.form.username }}"
|
||||||
|
id="username"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="input-field col s6">
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
placeholder="Password"
|
||||||
|
name="password"
|
||||||
|
value="{{
|
||||||
|
request.form.password }}"
|
||||||
|
id="password"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="center">
|
||||||
|
<button
|
||||||
|
class="waves-effect waves-light btn blue darken-2"
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
|
Create Account
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="card-panel grey darken-3">
|
||||||
|
{% for item in content_text %}
|
||||||
|
<p class="blue-grey-text text-lighten-5">
|
||||||
|
{{ item }}
|
||||||
|
</p>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
|
@ -5,13 +5,15 @@ except:
|
||||||
from lib.classUser import User
|
from lib.classUser import User
|
||||||
from lib.helpers import get_static_paths, get_content_text
|
from lib.helpers import get_static_paths, get_content_text
|
||||||
from . import app
|
from . import app
|
||||||
from flask import Flask, render_template, redirect, url_for, request
|
from flask import Flask, render_template, redirect, url_for, request, flash
|
||||||
from flask_login import LoginManager, UserMixin, login_required
|
from flask_login import LoginManager, UserMixin, login_required, login_user, current_user, logout_user
|
||||||
|
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
login_manager.init_app(app)
|
login_manager.init_app(app)
|
||||||
|
login_manager.login_view = "login"
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
@login_manager.user_loader
|
@login_manager.user_loader
|
||||||
def load_user(request):
|
def load_user(request):
|
||||||
token = request.headers.get('Authorization')
|
token = request.headers.get('Authorization')
|
||||||
|
@ -27,43 +29,71 @@ def load_user(request):
|
||||||
return user
|
return user
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
@login_manager.user_loader
|
||||||
|
def load_user(request):
|
||||||
|
return User(request, User.user_database[request][1])
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
|
||||||
@app.route('/home', methods=['GET'])
|
@app.route('/home', methods=['GET'])
|
||||||
|
@login_required
|
||||||
def home():
|
def home():
|
||||||
return render_template('home/index.html', user='admin', static_paths=get_static_paths(), content_text=get_content_text())
|
return render_template('home/index.html', static_paths=get_static_paths(), content_text=get_content_text())
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/signup', methods=['GET', 'POST'])
|
||||||
|
def signup():
|
||||||
|
error = 'Invalid method.'
|
||||||
|
if(request.method == 'POST'):
|
||||||
|
username = request.form['username']
|
||||||
|
password = request.form['password']
|
||||||
|
if(not username in User.user_database.keys()):
|
||||||
|
userClass = User(username, password)
|
||||||
|
User.user_database[username] = (username, password)
|
||||||
|
return redirect('/')
|
||||||
|
else:
|
||||||
|
flash('It looks like a user already exists with that name.')
|
||||||
|
return redirect('/signup')
|
||||||
|
elif(request.method == 'GET'):
|
||||||
|
return render_template('signup/index.html', static_paths=get_static_paths(), content_text=get_content_text())
|
||||||
|
return f'ERROR: {error}'
|
||||||
|
|
||||||
|
|
||||||
@app.route('/login', methods=['GET', 'POST'])
|
@app.route('/login', methods=['GET', 'POST'])
|
||||||
def login():
|
def login():
|
||||||
|
error = 'Invalid method.'
|
||||||
if(request.method == 'POST'):
|
if(request.method == 'POST'):
|
||||||
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
|
username = request.form['username']
|
||||||
error = 'Invalid Credentials. Please try again.'
|
password = request.form['password']
|
||||||
|
if(username in User.user_database.keys()):
|
||||||
|
userClass = User(username, password)
|
||||||
|
login_user(userClass)
|
||||||
|
return redirect('home')
|
||||||
else:
|
else:
|
||||||
return redirect(url_for('home'))
|
error = 'Invalid credentials.'
|
||||||
elif(request.method == 'GET'):
|
elif(request.method == 'GET'):
|
||||||
return render_template('login/index.html', static_paths=get_static_paths(), content_text=get_content_text())
|
return render_template('login/index.html', static_paths=get_static_paths(), content_text=get_content_text())
|
||||||
return 'ERROR: Invalid method.'
|
return f'ERROR: {error}'
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", methods=['GET'])
|
@app.route('/logout', methods=['GET', 'POST'])
|
||||||
|
def logout():
|
||||||
|
error = 'Invalid method.'
|
||||||
|
if(request.method == 'GET'):
|
||||||
|
logout_user()
|
||||||
|
return redirect('/')
|
||||||
|
return f'ERROR: {error}'
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=['GET'])
|
||||||
def hello():
|
def hello():
|
||||||
return render_template('homepage.html', static_paths=get_static_paths(), content_text=get_content_text())
|
if(not current_user.is_active):
|
||||||
|
return render_template('homepage.html', static_paths=get_static_paths(), content_text=get_content_text())
|
||||||
|
else:
|
||||||
|
return redirect('home')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
app.run(host='0.0.0.0', debug=True)
|
app.run(host='0.0.0.0', debug=True)
|
||||||
|
|
||||||
|
|
||||||
'''
|
|
||||||
@app.route("/", methods=["GET"])
|
|
||||||
def index():
|
|
||||||
return Response(response="Hello World!", status=200)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/protected/", methods=["GET"])
|
|
||||||
@login_required
|
|
||||||
def protected():
|
|
||||||
return Response(response="Hello Protected World!", status=200)
|
|
||||||
'''
|
|
||||||
|
|
Reference in a new issue