finyally get the DB done
just wannya go home
This commit is contained in:
parent
0e8121fcdb
commit
8c270bd24b
3 changed files with 60 additions and 67 deletions
59
app/app.py
59
app/app.py
|
@ -1,14 +1,14 @@
|
|||
from urllib import request
|
||||
|
||||
from flask import *
|
||||
from flask import request, redirect, session, render_template, send_from_directory, Flask
|
||||
from os import path, walk
|
||||
|
||||
import hashlib
|
||||
|
||||
import configparser
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, Column, Integer, String
|
||||
from sqlalchemy.orm import scoped_session,sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.sql import text
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
|
@ -20,8 +20,18 @@ databaseUsername = str(config['DATABASE']['username'])
|
|||
databasePassword = str(config['DATABASE']['password'])
|
||||
databaseName = str(config['DATABASE']['name'])
|
||||
|
||||
engine=create_engine("postgresql://" + databaseUsername + ":" + databasePassword + "@localhost/" + databaseName)
|
||||
db=scoped_session(sessionmaker(bind=engine))
|
||||
engine = create_engine(f"postgresql://{databaseUsername}:{databasePassword}@localhost/{databaseName}")
|
||||
db = scoped_session(sessionmaker(bind=engine))
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'users'
|
||||
id = Column(Integer, primary_key=True)
|
||||
username = Column(String, unique=True, nullable=False)
|
||||
password = Column(String, nullable=False)
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
@ -40,14 +50,47 @@ def home():
|
|||
return render_template('index.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
||||
|
||||
|
||||
@app.route('/auth/login/')
|
||||
@app.route('/auth/login/', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if request.method == 'POST':
|
||||
username = request.form['username']
|
||||
password = encrypt(request.form['password'])
|
||||
|
||||
user = db.execute(text("SELECT * FROM users WHERE username = :username AND password = :password"),
|
||||
{"username": username, "password": password}).fetchone()
|
||||
|
||||
if user:
|
||||
session['user_id'] = user.id
|
||||
return redirect('/')
|
||||
else:
|
||||
return "Invalid credentials!"
|
||||
return render_template('login.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
||||
|
||||
@app.route('/auth/register/')
|
||||
@app.route('/auth/register/', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if request.method == 'POST':
|
||||
username = request.form['username']
|
||||
password = encrypt(request.form['password'])
|
||||
print(username)
|
||||
|
||||
# check if username exists
|
||||
existing_user = db.execute(text("SELECT * FROM users WHERE username = :username"),
|
||||
{"username": username}).fetchone()
|
||||
if existing_user:
|
||||
return "User already exists!"
|
||||
|
||||
# insert new user
|
||||
db.execute(text("INSERT INTO users (username, password) VALUES (:username, :password)"),
|
||||
{"username": username, "password": password})
|
||||
db.commit()
|
||||
return redirect('/auth/login/') # redirect to login page after successful registration
|
||||
return render_template('register.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
||||
|
||||
@app.route('/auth/logout/')
|
||||
def logout():
|
||||
session.pop('user_id', None)
|
||||
return redirect('/')
|
||||
|
||||
@app.route('/assets/css/index.css')
|
||||
def index_css():
|
||||
return send_from_directory('static/assets/css', 'index.css')
|
||||
|
|
|
@ -11,19 +11,11 @@
|
|||
<h1>
|
||||
Login to {{ instanceLocation }}'s Nyxask!
|
||||
</h1>
|
||||
<label for="name">Username: </label>
|
||||
<input
|
||||
type="text"
|
||||
id="name"
|
||||
name="name"
|
||||
size="10" />
|
||||
<br>
|
||||
<label for="password">Password: </label>
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
size="10" />
|
||||
<form method="POST">
|
||||
<input type="text" name="username" placeholder="Username" required>
|
||||
<input type="password" name="password" placeholder="Password" required>
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -11,52 +11,10 @@
|
|||
<h1>
|
||||
Register at {{ instanceLocation }}
|
||||
</h1>
|
||||
<form>
|
||||
<label>
|
||||
<input
|
||||
placeholder="username"
|
||||
type="text"
|
||||
name="username"
|
||||
size="10"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<br>
|
||||
<br>
|
||||
<label>
|
||||
<input
|
||||
placeholder="password"
|
||||
type="password"
|
||||
name="password"
|
||||
size="10"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<input
|
||||
placeholder="confirm password"
|
||||
type="password"
|
||||
name="confirm"
|
||||
size="10"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<br>
|
||||
<br>
|
||||
<label>
|
||||
Agree to the rules
|
||||
<input
|
||||
type="checkbox"
|
||||
name="scales"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<br>
|
||||
<br>
|
||||
<input
|
||||
type="submit"
|
||||
value="Register"
|
||||
/>
|
||||
<form method="POST">
|
||||
<input type="text" name="username" placeholder="Username" required>
|
||||
<input type="password" name="password" placeholder="Password" required>
|
||||
<button type="submit">register</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
|
|
Loading…
Add table
Reference in a new issue