My requirement is when i enter the username and passwords saved in my database, the validation should lead me to respective dashboards according to the roles but somehow when i enter id and pass saved in my database or even a random id and pass, i get redirected to the print statement 'Something is wrong'. Here is my code of FRS.py, please tell me what the logic problem is, thanks.
from flask import Flask, render_template, redirect, request, session, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user
import bcrypt
app = Flask(__name__, template_folder='templates')
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///FRS_database.db"
app.config['SECRET_KEY'] = 'my_secret_key'
db: SQLAlchemy = SQLAlchemy(app)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
role = db.Column(db.String(10), nullable=False)
def __init__(self, username, password, role):
self.username = username
self.role = role
self.password = password
# Check if the password is already hashed
if not password.startswith("$2b$"):
# Hash the password
self.password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
else:
# Use the existing hashed password
self.password = password
def check_password(self, password):
return bcrypt.checkpw(password.encode('utf-8'), self.password.encode('utf-8'))
with app.app_context():
db.create_all()
@app.route("/")
def Home():
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']
role = request.form['role']
new_user = User(username=username, password=password, role=role)
db.session.add(new_user)
db.session.commit()
return redirect('/Login')
return render_template('Register.html')
@app.route("/Login", methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
if user and user.check_password(password):
login_user(user)
session['username'] = user.username
session['id'] = user.id
session['password'] = user.password
session['role'] = user.role
if 'role' in session:
if session['role'] == 'admin':
return redirect(url_for('admin_dashboard'))
elif session['role'] == 'student':
return redirect(url_for('student_dashboard'))
elif session['role'] == 'faculty':
return redirect(url_for('faculty_dashboard'))
else:
print("Role in session:", session['role'])
return render_template('Login.html')
flash('Invalid username or password', 'error')
return 'something is wrong'
return 'oops'
@app.route('/admin_dashboard')
def admin_dashboard():
if 'role' in session and session['role'] == 'admin':
return render_template('admin_dashboard.html')
return redirect(url_for('Login'))
@app.route('/student_dashboard')
def student_dashboard():
if 'role' in session and session['role'] == 'student':
return render_template('student_dashboard.html')
return redirect(url_for('Login'))
@app.route('/faculty_dashboard')
def faculty_dashboard():
if 'role' in session and session['role'] == 'faculty':
return render_template('faculty_dashboard.html')
return redirect(url_for('Login'))
@app.route('/logout')
def logout():
return render_template('Login.html')
if __name__ == "__main__":
app.run(debug=True)
i've tried almost everything in my knowledge and the problem of redirecting to respective dashboard still persists. i think there might be something wrong with the login logic or the database