I'm building a quick registration page with Firebase, Pyrebase & Flask; I have managed to create a user account & push the data to the business folder; I have also created a login where the user is logged into a profile page - to display their name & their name of business.
My only problem is that I cannot extract the single data, but instead it displays the data as a single block; this makes it hard for me to find a way just to extract only the businessName
or just the userName
from the registerList
.
I also want only the information to display just the user who has signed in - I think I need to use some type of user[idToken]
I'm not sure.. Instead I get the whole list (I know using a for-loop doesn't help!)
registration.py
import pyrebase
from flask import *
app = Flask(__name__)
config = {
"apiKey": "",
"authDomain": "",
"databaseURL": "",
"projectId": "",
"storageBucket": "",
"messagingSenderId": "",
"appId": "",
"measurementId": ""
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
db = firebase.database()
@app.route('/', methods=['GET', 'POST'])
def login():
unsuccessful = 'Please check your credentials'
successful = 'Login successful'
if request.method == 'POST':
email = request.form.get('name')
password = request.form.get('pass')
try:
user = auth.sign_in_with_email_and_password(email, password)
register = db.child("Bame_Register").child("business").get()
registerList = register.val()
return render_template('profile.html', registerList=registerList)
except:
return render_template('new.html', us=unsuccessful)
return render_template('new.html', us=unsuccessful)
@app.route('/register', methods=['GET', 'POST'])
def register():
unsuccessful = 'Please check your credentials'
successful = 'Registraion successful'
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('pass')
userName = request.form.get('inputName')
businessName = request.form.get('businessName')
try:
user = auth.create_user_with_email_and_password(email, password)
auth.send_email_verification(user['idToken'])
bameRegister = dict(userName=userName, businessName=businessName)
db.child("Bame_Register").child("business").push(bameRegister)
return render_template('new.html', x=successful)
except:
return render_template('new.html', y=unsuccessful)
return render_template('new.html')
if __name__ == '__main__':
app.run(debug=True)
new.html
<!DOCTYPE html>
<html>
<head>
<title>Flask Firebase Auth</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
{% if s %}
<div class="alert alert-success">
<h2>{{s}}</h2>
</div>
{% endif %}
{% if us %}
<div class="alert alert-danger">
<h2>{{us}}</h2>
</div>
{% endif %}
<form action="/" method="post">
<h2>Please sign in</h2>
<label for="inputEmail">Email address</label>
<input type="email" id="inputEmail" name="name" placeholder="Email address" required autofocus>
<label for="inputPassword">Password</label>
<input type="password" id="inputPassword" name="pass" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<br>
<div class="container">
{% if x %}
<div class="alert alert-success">
<h2>{{x}}</h2>
</div>
{% endif %}
{% if y %}
<div class="alert alert-danger">
<h2>{{y}}</h2>
</div>
{% endif %}
<form action="{{ url_for('register') }}" method="post">
<h2>Please create an account to register your business</h2>
<label for="inputEmail">Email address</label>
<input type="email" id="inputEmail" name="email" placeholder="Email address" required autofocus>
<label for="inputPassword">Password</label>
<input type="password" id="inputPassword" name="pass" placeholder="Password" required>
<br>
<label for="inputName">Enter your name: </label>
<input type="text" id="inputName" name="inputName" placeholder="Name" required>
<label for="businessName">Business Name</label>
<input type="text" id="businessName" name="businessName" placeholder="Business Name" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Register business</button>
</form>
</div>
</body>
</html>
profile.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>dynamic pages</title>
</head>
<body>
{% for key,value in registerList.items() %}
{% for data in value.values() %}
<h4>{{ data }}</h4>
{% endfor %}
{% endfor %}
<h6>Dynamic profile page</h6>
</body>
</html>
How would I display just the name of the user or just their business name? I was thinking maybe data[userName]
or data[businessName]
, but that didn't work! I want to be able to take the data separately so I can control where I display the data. The data displayed should only be of the user who has logged in. Then I can later add functionality where the users can edit their information.