Browse Source

Every start is hard

master
Domagoj Zecevic 2 years ago
commit
725f1844cc
  1. 66
      app.py
  2. 28
      templates/home.html
  3. 30
      templates/login.html
  4. 32
      templates/sign_up.html

66
app.py

@ -0,0 +1,66 @@
from flask import Flask, render_template, request, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite"
app.config["SECRET_KEY"] = "abc"
db = SQLAlchemy()
login_manager = LoginManager()
login_manager.init_app(app)
class Users(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(250), unique=True, nullable=False)
password = db.Column(db.String(250), nullable=False)
db.init_app(app)
with app.app_context():
db.create_all()
@login_manager.user_loader
def loader_user(user_id):
return Users.query.get(user_id)
@app.route('/register', methods=["GET", "POST"])
def register():
if request.method == "POST":
user = Users(username=request.form.get("username"),
password=request.form.get("password"))
db.session.add(user)
db.session.commit()
return redirect(url_for("login"))
return render_template("sign_up.html")
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
user = Users.query.filter_by(
username=request.form.get("username")).first()
if user.password == request.form.get("password"):
login_user(user)
return redirect(url_for("home"))
return render_template("login.html")
@app.route("/logout")
def logout():
logout_user()
return redirect(url_for("home"))
@app.route("/")
def home():
return render_template("home.html")
if __name__ == "__main__":
app.run()

28
templates/home.html

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Home</title>
<style>
h1 {
color: green;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/register">Create account</a></li>
</ul>
</nav>
{% if current_user.is_authenticated %}
<h1>You are logged in</h1>
<a href="/logout">Logout</a>
{% else %}
<h1>You are not logged in</h1>
{% endif %}
</body>
</html>

30
templates/login.html

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login</title>
<style>
h1{
color: green;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/register">Create account</a></li>
</ul>
</nav>
<h1>Login to your account</h1>
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" name="username" />
<label for="password">Password:</label>
<input type="password" name="password" />
<button type="submit">Submit</button>
</form>
</body>
</html>

32
templates/sign_up.html

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sign Up</title>
<style>
h1 {
color: green;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/login">Login</a></li>
<li><a href="/register">Create account</a></li>
</ul>
</nav>
<h1>Create an account</h1>
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" name="username" />
<label for="password">Password:</label>
<input type="password" name="password" />
<label for="name">name:</label>
<input type="text" name="name" />
<button type="submit">Submit</button>
</form>
</body>
</html>