Skip to content

Commit

Permalink
important comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
MBSA-INFINITY committed Jul 14, 2023
1 parent 1aa3adf commit 7a80f97
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Firebase_Authentication_Using_Flask/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#Importing Firebase
from firebase import Firebase
import os

#Intializing Firebase Configuration from the Environment Variables
config = {
"apiKey": os.environ.get("FIREBASE_APIKEY"),
"authDomain": os.environ.get("FIREBASE_AUTHDOMAIN"),
Expand All @@ -12,6 +14,11 @@
"measurementId": os.environ.get("FIREBASE_MEASUREMENT_ID")
}

#Intializing Firebase Object
firebase = Firebase(config)

#Intializing Firebase Databse
db = firebase.database()

#Intializing Firebase Auth
auth = firebase.auth()
22 changes: 20 additions & 2 deletions Firebase_Authentication_Using_Flask/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#Importing Flask and other important functions
from flask import Flask, render_template, request, redirect, abort, flash, session ,url_for
#Importing firebase auth from db.py
from db import auth

app = Flask(__name__)
app.secret_key = "MBSAIADITYA"

exempted_endpoints = ['signup','login','static']

'''
Signup Route
'''
@app.route("/signup", methods = ['GET','POST'])
def signup():
if request.method=='POST':
Expand All @@ -16,6 +21,7 @@ def signup():
if password == repassword:
if len(password)>=6:
try:
#Creating User in firebase using create_user_with_email_and_password method of firebase/auth
_user_ = auth.create_user_with_email_and_password(username ,password)
flash("User has been created successfully! Please Login")
return redirect("/")
Expand All @@ -29,13 +35,17 @@ def signup():
return redirect("/signup")
return render_template("signup.html")

'''
Login Route
'''
@app.route("/login",methods = ['GET','POST'] )
def login():
if request.method == 'POST':
data = dict(request.form)
email = data.get("email")
password = data.get("password")
try:
#Signing User in firebase using sign_in_with_email_and_password method of firebase/auth
user = auth.sign_in_with_email_and_password(email ,password)
print(user)
session['user'] = user['localId']
Expand All @@ -48,12 +58,16 @@ def login():
return redirect("/")
return render_template("login.html")


'''
Main dashboard route which has to be protected
'''
@app.route("/",methods = ['GET','POST'])
def start():
return render_template("index.html", user=session['email'])


'''
Logout Route
'''
@app.route("/logout",methods = ['GET','POST'])
def logout():
session.pop('user')
Expand All @@ -62,6 +76,10 @@ def logout():
return redirect("/")


'''This is an important middleware that run before any request made to flask application and checks
when user is authenticated or not!
'''

@app.before_request
def before_request_func():
if request.endpoint in exempted_endpoints:
Expand Down

0 comments on commit 7a80f97

Please sign in to comment.