Skip to content

Commit c65eb2f

Browse files
committed
flask login code
1 parent e468d5f commit c65eb2f

File tree

10 files changed

+153
-0
lines changed

10 files changed

+153
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from project_awesome import app
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
import os
4+
5+
app = Flask(__name__)
6+
db = SQLAlchemy(app)
7+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site_users.db'
8+
app.secret_key = os.urandom(12)
9+
10+
from project_awesome import routes, models
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
from flask_login import UserMixin
3+
from project_awesome import db
4+
5+
class User(UserMixin, db.Model):
6+
id = db.Column(db.Integer, primary_key=True)
7+
username = db.Column(db.String(80), unique=True, nullable=False)
8+
password = db.Column(db.String(120), unique=False, nullable=False)
9+
10+
def __repr__(self):
11+
return 'User {}'.format(self.username)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from flask import render_template, request, flash, url_for, redirect
2+
from flask_login import LoginManager, login_required, login_user, UserMixin, logout_user
3+
from project_awesome import app, db
4+
from project_awesome.models import User
5+
6+
login_manager = LoginManager()
7+
login_manager.init_app(app)
8+
login_manager.login_view = 'loginpage'
9+
10+
@app.route('/')
11+
@app.route('/index')
12+
def index():
13+
return render_template('index.html')
14+
15+
@app.route('/pybitesdashboard')
16+
@login_required
17+
def pybitesdashboard():
18+
return render_template('pybitesdashboard.html')
19+
20+
@app.route('/loginpage', methods=['GET', 'POST'])
21+
def loginpage():
22+
if request.method == 'POST' and 'username' in request.form:
23+
user = User.query.filter_by(username=request.form.get('username')).first()
24+
if user:
25+
if user.password == request.form.get('password'):
26+
login_user(user)
27+
return redirect(url_for('pybitesdashboard'))
28+
return 'Invalid username or password'
29+
30+
return render_template('loginpage.html')
31+
32+
@app.route('/logoutpage')
33+
@login_required
34+
def logoutpage():
35+
logout_user()
36+
return redirect(url_for('index'))
37+
38+
@app.route('/create_user', methods=['GET', 'POST'])
39+
def create_user():
40+
if request.method == 'POST' and 'username' in request.form:
41+
username = request.form.get('username')
42+
password = request.form.get('password')
43+
add_user(username, password)
44+
return render_template('create_user.html')
45+
46+
47+
def add_user(username, password):
48+
user = User(username=username, password=password)
49+
db.session.add(user)
50+
db.session.commit()
51+
flash('User Created')
52+
53+
@login_manager.user_loader
54+
def load_user(user_id):
55+
return User.query.get(int(user_id))
56+
57+
if __name__ == '__main__':
58+
pass
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html>
2+
<head>
3+
<title>Create User Page</title>
4+
{% with messages = get_flashed_messages() %}
5+
{% if messages %}
6+
<ul class=flashes>
7+
{% for message in messages %}
8+
<li>{{ message }}</li>
9+
{% endfor %}
10+
</ul>
11+
{% endif %}
12+
{% endwith %}
13+
<head>
14+
15+
<body>
16+
<h1>This is a page where we can create a new user</h1>
17+
18+
<div>
19+
<form method="POST" action="/create_user">
20+
<label for="User Creation">Create your User</label><br>
21+
<input type="text" name="username" placeholder="Eg: TheRealBob"><br>
22+
<input type="text" name="password" placeholder="Eg: not-abc123"><br>
23+
<button type="submit" value="Submit">Submit</button>
24+
</form>
25+
</div>
26+
</body>
27+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Home Page</title>
4+
</head>
5+
6+
<body>
7+
<h1>This is the home page, you do not need to be logged in to access this page</h1>
8+
</body>
9+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<head>
3+
<title>Login Page</title>
4+
</head>
5+
6+
<body>
7+
<h1>This is the page where users will login to the site</h1>
8+
9+
<div>
10+
<form method="POST">
11+
<label for="User login">Login</label><br>
12+
<input type="text" name="username" placeholder="Eg: TheRealBob"><br>
13+
<input type="text" name="password"><br>
14+
<button type="submit" value="Submit">Submit</button>
15+
</form>
16+
</div>
17+
</body>
18+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>PyBites Dashboard</title>
4+
</head>
5+
6+
<body>
7+
<h1>This is the PyBites Dashboard. You can only access this page once logged in</h1>
8+
</body>
9+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Click==7.0
2+
Flask==1.0.2
3+
Flask-Login==0.4.1
4+
Flask-SQLAlchemy==2.3.2
5+
itsdangerous==1.1.0
6+
Jinja2==2.10
7+
MarkupSafe==1.1.0
8+
python-dotenv==0.10.1
9+
SQLAlchemy==1.2.15
10+
Werkzeug==0.14.1

0 commit comments

Comments
 (0)