-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (106 loc) · 4.5 KB
/
main.py
File metadata and controls
118 lines (106 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
################################################################################
################################################################################
######## ########
######## Python - Firebase - Flask Login/Register App ########
######## Author: Hemkesh Agrawal ########
######## Website: http://hemkesh.com ########
######## Last updated on: 11/27/2019 ########
######## ########
######## P.S. This is my first ever github project, so I ########
######## would love to hear your feedback : agrawalh@msu.edu ########
######## ########
################################################################################
################################################################################
import pyrebase
from flask import Flask, flash, redirect, render_template, request, session, abort, url_for
app = Flask(__name__) #Initialze flask constructor
#Add your own details
config = {
"apiKey": "PASTE_HERE",
"authDomain": "PASTE_HERE",
"databaseURL": "PASTE_HERE",
"storageBucket": "PASTE_HERE"
}
#initialize firebase
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
db = firebase.database()
#Initialze person as dictionary
person = {"is_logged_in": False, "name": "", "email": "", "uid": ""}
#Login
@app.route("/")
def login():
return render_template("login.html")
#Sign up/ Register
@app.route("/signup")
def signup():
return render_template("signup.html")
#Welcome page
@app.route("/welcome")
def welcome():
if person["is_logged_in"] == True:
return render_template("welcome.html", email = person["email"], name = person["name"])
else:
return redirect(url_for('login'))
#If someone clicks on login, they are redirected to /result
@app.route("/result", methods = ["POST", "GET"])
def result():
if request.method == "POST": #Only if data has been posted
result = request.form #Get the data
email = result["email"]
password = result["pass"]
try:
#Try signing in the user with the given information
user = auth.sign_in_with_email_and_password(email, password)
#Insert the user data in the global person
global person
person["is_logged_in"] = True
person["email"] = user["email"]
person["uid"] = user["localId"]
#Get the name of the user
data = db.child("users").get()
person["name"] = data.val()[person["uid"]]["name"]
#Redirect to welcome page
return redirect(url_for('welcome'))
except:
#If there is any error, redirect back to login
return redirect(url_for('login'))
else:
if person["is_logged_in"] == True:
return redirect(url_for('welcome'))
else:
return redirect(url_for('login'))
#If someone clicks on register, they are redirected to /register
@app.route("/register", methods = ["POST", "GET"])
def register():
if request.method == "POST": #Only listen to POST
result = request.form #Get the data submitted
email = result["email"]
password = result["pass"]
name = result["name"]
try:
#Try creating the user account using the provided data
auth.create_user_with_email_and_password(email, password)
#Login the user
user = auth.sign_in_with_email_and_password(email, password)
#Add data to global person
global person
person["is_logged_in"] = True
person["email"] = user["email"]
person["uid"] = user["localId"]
person["name"] = name
#Append data to the firebase realtime database
data = {"name": name, "email": email}
db.child("users").child(person["uid"]).set(data)
#Go to welcome page
return redirect(url_for('welcome'))
except:
#If there is any error, redirect to register
return redirect(url_for('register'))
else:
if person["is_logged_in"] == True:
return redirect(url_for('welcome'))
else:
return redirect(url_for('register'))
if __name__ == "__main__":
app.run()