Skip to content

Instantly share code, notes, and snippets.

@abhishek-shukla21
Created July 14, 2023 12:38
Show Gist options
  • Save abhishek-shukla21/923c47d054183288cc0fdbe90374f682 to your computer and use it in GitHub Desktop.
Save abhishek-shukla21/923c47d054183288cc0fdbe90374f682 to your computer and use it in GitHub Desktop.
Flask Application with Database Connection and Authentication
from flask import Flask, render_template, request, redirect, session
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'your_database_uri_here' # Replace with your database URI
app.config['SECRET_KEY'] = 'your_secret_key_here' # Replace with your secret key
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
def __init__(self, username, password):
self.username = username
self.password = password
@app.route('/')
def home():
if 'username' in session:
return f'Hello, {session["username"]}! You are logged in.'
return 'Home Page'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password, password):
session['username'] = username
return redirect('/')
else:
return 'Invalid username or password'
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
hashed_password = generate_password_hash(password)
user = User(username=username, password=hashed_password)
db.session.add(user)
db.session.commit()
return redirect('/login')
return render_template('register.html')
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect('/')
if __name__ == '__main__':
db.create_all()
app.run()
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
<p>Don't have an account? <a href="/register">Register here</a></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="/register" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Register">
</form>
<p>Already have an account? <a href="/login">Login here</a></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment