Creating a login Register login/Register with  session authentication using Python Flask

In This, We can learn How to make a  login/Register with  session authentication using Flask

1. APP.PY

from flask import Flask, render_template, request, redirect, session,get_flashed_messages, flash
import mysql.connector
import os
import random
app=Flask(__name__)
app.secret_key=os.urandom(24)

 

conn= mysql.connector.connect(host=”localhost”, user=”root”, password=”Batman”, database=”flasklogin”)
cursor= conn.cursor()
@app.route(‘/’)
def home():
    return render_template(“main.html”)
@app.route(‘/login’)
def login():
    if session.get(‘user_id’):   # Check session exists or not
        return redirect(“/dashboard”)
    else:
        return render_template(“login.html”)
   

 

@app.route(‘/register’)
def register():
    if session.get(‘user_id’):
        return redirect(“/dashboard”)
    else:
        return render_template(“register.html”)

 

@app.route(‘/dashboard’)
def dashboard():
    if session.get(‘user_id’):
        user_name=session.get(‘user_id’)
        return render_template(“dashboard.html”, user=user_name)
    else:
        return redirect(‘/login’)
# ———-User Login(User Login Validation)—————-

 

@app.route(‘/login_validation’, methods =[‘POST’])
def login_validation():
    username=request.form.get(‘uname’)
    password=request.form.get(‘psw’)
   
    cursor.execute(“””Select * from users where username LIKE ‘{}’ AND password LIKE ‘{}'”””.format(username, password))
    users=cursor.fetchall()  #–Fetch All data from database
    if len(users)>0:         # if matches then a tuple gets in output if not then blank tuple
        session[‘user_id’]= users[0][1]  # Create a session with existing username or make with userid
        flash(“Login Successfull”)
        return redirect (‘/dashboard’)
    else:
        flash(“Invalid credentials. Check your account username and password”)
        return redirect (‘/login’)
       
# ———-User Register(Adding a new user)—————-

 

@app.route(‘/add_user’, methods =[‘POST’])
def add_user():
    name=request.form.get(‘rname’)
    email=request.form.get(‘remail’)
    username=request.form.get(‘runame’)
    password=request.form.get(‘rpsw’)
    cursor.execute(“””Select * from users where username LIKE ‘{}’ OR email LIKE ‘{}'”””.format(username, email))
    users=cursor.fetchall()
    if len(users)>0:
        flash(“That username or email is already used, please choose another”)
        return redirect(‘/register’)
    else:
        cursor.execute(“””INSERT INTO users (name, email, username, password)
                            VALUES
                            (‘{}’,'{}’,'{}’,'{}’) “””.format(name, email, username, password))
        conn.commit()
        flash(“User Registered Successfully”)
        return redirect (‘/login’)

 

@app.route(‘/logout’)
def logout():
    session.pop(‘user_id’)
    return redirect(‘/login’)
if __name__== ‘__main__’:
    app.run(debug=True)

Leave a Comment

Your email address will not be published. Required fields are marked *