Merge pull request #1 from LEG-Industries/registration

User registration
This commit is contained in:
Benjamin Ramhorst 2022-02-06 11:47:30 +00:00 committed by GitHub
commit 46c544d953
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

73
app.py
View file

@ -1,16 +1,79 @@
from flask import Flask import json
from random import randint
import firebase_admin import firebase_admin
from firebase_admin import credentials from flask import Flask, Response, request
from firebase_admin import firestore from flask_mail import Mail, Message
from firebase_admin import credentials, firestore, auth
from firebase_admin._auth_utils import EmailAlreadyExistsError
# Initialize Flask app
app = Flask(__name__) app = Flask(__name__)
# Initalize Mail instance
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'legbarkr@gmail.com'
app.config['MAIL_PASSWORD'] = '!Password123'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
# Initialize Firebase
firebase = firebase_admin.initialize_app(credentials.Certificate('firebase-key.json')) firebase = firebase_admin.initialize_app(credentials.Certificate('firebase-key.json'))
@app.route('/') @app.route('/')
def hello_world(): # put application's code here def hello_world():
return 'Hello World! I am the dog you tracked with the amazing sensor from LEG industries...kidding this is just the home page :)' return 'Hello World! I am the dog you tracked with the amazing sensor from LEG industries...kidding this is just the home page :)'
@app.route('/auth/register', methods=['POST'])
def register():
body = request.json
if body is None:
return Response("{'error':'Invalid request - please provide a body'}", status=400, mimetype='application/json')
email = body['email']
password = body['password']
name = body['name']
deviceId = body['deviceid']
# Some fields are not present
if email is None or password is None or name is None or deviceId is None:
return Response("{'error':'Entries missing'}", status=400, mimetype='application/json')
# Register user with Firebase authentication
try:
user = auth.create_user(
email=email,
email_verified=False,
password=password,
display_name=name,
disabled=False)
except EmailAlreadyExistsError:
return Response("{'error':'User with given email address already exists'}", status=409, mimetype='application/json')
# Prompt the user to verify their email
code = randint(100000, 999999)
data = {
u'code': code
}
firestore.client().collection(u'verification').document(user.uid).set(data)
msg = Message('Please verify your email for BarkFinder', sender = 'TBA', recipients = [email])
msg.body = '''Hey {}! Thank you for signing up for BarkFinder.
In order to use our sevices, could you please verify your email address by logging in and entering this code {}'''.format(name, code)
mail.send(msg)
# Link the user to the device
data = {
u'devices': [deviceId]
}
firestore.client().collection(u'devices').document(user.uid).set(data)
# User successfully created and linked to device
resp = {"uid":user.uid}
return Response(json.dumps(resp), status=201, mimetype='application/json')
# An example of reading data from Firebase # An example of reading data from Firebase
# Taken from Firebase documentation # Taken from Firebase documentation
@app.route('/example/read', methods=['GET']) @app.route('/example/read', methods=['GET'])