2022-02-04 13:15:23 +00:00
|
|
|
import json
|
2022-02-06 11:45:29 +00:00
|
|
|
from random import randint
|
|
|
|
import firebase_admin
|
|
|
|
from flask import Flask, Response, request
|
|
|
|
from flask_mail import Mail, Message
|
2022-02-04 13:15:23 +00:00
|
|
|
from firebase_admin import credentials, firestore, auth
|
2022-02-06 11:45:29 +00:00
|
|
|
from firebase_admin._auth_utils import EmailAlreadyExistsError
|
2022-02-02 23:06:27 +00:00
|
|
|
|
2022-02-04 13:15:23 +00:00
|
|
|
# Initialize Flask app
|
2022-02-02 22:52:07 +00:00
|
|
|
app = Flask(__name__)
|
2022-02-04 13:15:23 +00:00
|
|
|
|
|
|
|
# Initalize Mail instance
|
|
|
|
app.config['MAIL_SERVER']='smtp.gmail.com'
|
|
|
|
app.config['MAIL_PORT'] = 465
|
2022-02-06 11:45:29 +00:00
|
|
|
app.config['MAIL_USERNAME'] = 'legbarkr@gmail.com'
|
|
|
|
app.config['MAIL_PASSWORD'] = '!Password123'
|
2022-02-04 13:15:23 +00:00
|
|
|
app.config['MAIL_USE_TLS'] = False
|
|
|
|
app.config['MAIL_USE_SSL'] = True
|
|
|
|
mail = Mail(app)
|
|
|
|
|
|
|
|
# Initialize Firebase
|
2022-02-02 23:19:39 +00:00
|
|
|
firebase = firebase_admin.initialize_app(credentials.Certificate('firebase-key.json'))
|
2022-02-02 22:52:07 +00:00
|
|
|
|
|
|
|
@app.route('/')
|
2022-02-04 13:15:23 +00:00
|
|
|
def hello_world():
|
2022-02-02 23:06:27 +00:00
|
|
|
return 'Hello World! I am the dog you tracked with the amazing sensor from LEG industries...kidding this is just the home page :)'
|
2022-02-02 22:52:07 +00:00
|
|
|
|
2022-02-06 11:45:29 +00:00
|
|
|
@app.route('/auth/register', methods=['POST'])
|
2022-02-04 13:15:23 +00:00
|
|
|
def register():
|
2022-02-06 11:45:29 +00:00
|
|
|
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']
|
2022-02-04 13:15:23 +00:00
|
|
|
|
|
|
|
# Some fields are not present
|
|
|
|
if email is None or password is None or name is None or deviceId is None:
|
2022-02-06 11:45:29 +00:00
|
|
|
return Response("{'error':'Entries missing'}", status=400, mimetype='application/json')
|
2022-02-04 13:15:23 +00:00
|
|
|
|
|
|
|
# Register user with Firebase authentication
|
2022-02-06 11:45:29 +00:00
|
|
|
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')
|
2022-02-04 13:15:23 +00:00
|
|
|
|
|
|
|
# Prompt the user to verify their email
|
|
|
|
code = randint(100000, 999999)
|
|
|
|
data = {
|
|
|
|
u'code': code
|
|
|
|
}
|
2022-02-06 11:45:29 +00:00
|
|
|
firestore.client().collection(u'verification').document(user.uid).set(data)
|
2022-02-04 13:15:23 +00:00
|
|
|
msg = Message('Please verify your email for BarkFinder', sender = 'TBA', recipients = [email])
|
2022-02-06 11:45:29 +00:00
|
|
|
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)
|
2022-02-04 13:15:23 +00:00
|
|
|
mail.send(msg)
|
|
|
|
|
|
|
|
# Link the user to the device
|
|
|
|
data = {
|
|
|
|
u'devices': [deviceId]
|
|
|
|
}
|
|
|
|
firestore.client().collection(u'devices').document(user.uid).set(data)
|
|
|
|
|
2022-02-06 11:45:29 +00:00
|
|
|
# User successfully created and linked to device
|
2022-02-04 13:15:23 +00:00
|
|
|
resp = {"uid":user.uid}
|
2022-02-06 11:45:29 +00:00
|
|
|
return Response(json.dumps(resp), status=201, mimetype='application/json')
|
2022-02-04 13:15:23 +00:00
|
|
|
|
|
|
|
|
2022-02-02 23:19:39 +00:00
|
|
|
# An example of reading data from Firebase
|
|
|
|
# Taken from Firebase documentation
|
|
|
|
@app.route('/example/read', methods=['GET'])
|
|
|
|
def exampleRead():
|
|
|
|
doc_ref = firestore.client().collection(u'cities').document(u'LA')
|
|
|
|
doc = doc_ref.get()
|
|
|
|
if doc.exists:
|
|
|
|
return f'Document data: {doc.to_dict()}'
|
|
|
|
else:
|
|
|
|
return u'No such document!'
|
|
|
|
|
|
|
|
# An example of saving data to Firebase
|
|
|
|
# Taken from Firebase documentation
|
|
|
|
# This should be a POST method...will change later
|
|
|
|
@app.route('/example/write', methods=['GET'])
|
|
|
|
def examnpleWrite():
|
|
|
|
data = {
|
|
|
|
u'name': u'Los Angeles',
|
|
|
|
u'state': u'CA',
|
|
|
|
u'country': u'USA'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add a new doc in collection 'cities' with ID 'LA'
|
|
|
|
firestore.client().collection(u'cities').document(u'LA').set(data)
|
|
|
|
return 'Saved to Firebase'
|
|
|
|
|
2022-02-02 22:52:07 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run()
|