mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-22 21:45:48 +00:00
Registration bug fixes
This commit is contained in:
parent
57dba312ab
commit
7e234da82a
56
app.py
56
app.py
|
@ -1,8 +1,10 @@
|
||||||
import json
|
import json
|
||||||
from random import randint
|
from random import randint
|
||||||
from flask import Flask, request
|
import firebase_admin
|
||||||
from flask_mail import Mail
|
from flask import Flask, Response, request
|
||||||
|
from flask_mail import Mail, Message
|
||||||
from firebase_admin import credentials, firestore, auth
|
from firebase_admin import credentials, firestore, auth
|
||||||
|
from firebase_admin._auth_utils import EmailAlreadyExistsError
|
||||||
|
|
||||||
# Initialize Flask app
|
# Initialize Flask app
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
@ -10,8 +12,8 @@ app = Flask(__name__)
|
||||||
# Initalize Mail instance
|
# Initalize Mail instance
|
||||||
app.config['MAIL_SERVER']='smtp.gmail.com'
|
app.config['MAIL_SERVER']='smtp.gmail.com'
|
||||||
app.config['MAIL_PORT'] = 465
|
app.config['MAIL_PORT'] = 465
|
||||||
#app.config['MAIL_USERNAME'] = ''
|
app.config['MAIL_USERNAME'] = 'legbarkr@gmail.com'
|
||||||
#app.config['MAIL_PASSWORD'] = ''
|
app.config['MAIL_PASSWORD'] = '!Password123'
|
||||||
app.config['MAIL_USE_TLS'] = False
|
app.config['MAIL_USE_TLS'] = False
|
||||||
app.config['MAIL_USE_SSL'] = True
|
app.config['MAIL_USE_SSL'] = True
|
||||||
mail = Mail(app)
|
mail = Mail(app)
|
||||||
|
@ -19,40 +21,46 @@ mail = Mail(app)
|
||||||
# Initialize Firebase
|
# 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():
|
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']):
|
@app.route('/auth/register', methods=['POST'])
|
||||||
def register():
|
def register():
|
||||||
email = request.form.get('email')
|
body = request.json
|
||||||
password = request.form.get('email')
|
|
||||||
name = request.form.get('name')
|
if body is None:
|
||||||
deviceId = request.form.get('deviceid')
|
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
|
# Some fields are not present
|
||||||
if email is None or password is None or name is None or deviceId is None:
|
if email is None or password is None or name is None or deviceId is None:
|
||||||
return Response("{'error':'Entires missing'}", status=400, mimetype='application/json')
|
return Response("{'error':'Entries missing'}", status=400, mimetype='application/json')
|
||||||
|
|
||||||
# Register user with Firebase authentication
|
# Register user with Firebase authentication
|
||||||
user = auth.create_user(
|
try:
|
||||||
email=email
|
user = auth.create_user(
|
||||||
email_verified=False,
|
email=email,
|
||||||
password=password,
|
email_verified=False,
|
||||||
display_name=name,
|
password=password,
|
||||||
disabled=False)
|
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
|
# Prompt the user to verify their email
|
||||||
code = randint(100000, 999999)
|
code = randint(100000, 999999)
|
||||||
data = {
|
data = {
|
||||||
u'code': code
|
u'code': code
|
||||||
}
|
}
|
||||||
firestore.client().collection(u'verification').document(user.uid).set(code)
|
firestore.client().collection(u'verification').document(user.uid).set(data)
|
||||||
msg = Message('Please verify your email for BarkFinder', sender = 'TBA', recipients = [email])
|
msg = Message('Please verify your email for BarkFinder', sender = 'TBA', recipients = [email])
|
||||||
msg.body = '''Hey {}! Thank you for signing up for BarkFinder.
|
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 {}'''
|
In order to use our sevices, could you please verify your email address by logging in and entering this code {}'''.format(name, code)
|
||||||
.format(name, code)
|
|
||||||
mail.send(msg)
|
mail.send(msg)
|
||||||
|
|
||||||
# Link the user to the device
|
# Link the user to the device
|
||||||
|
@ -61,9 +69,9 @@ def register():
|
||||||
}
|
}
|
||||||
firestore.client().collection(u'devices').document(user.uid).set(data)
|
firestore.client().collection(u'devices').document(user.uid).set(data)
|
||||||
|
|
||||||
# User succesfully created and linked to device
|
# User successfully created and linked to device
|
||||||
resp = {"uid":user.uid}
|
resp = {"uid":user.uid}
|
||||||
return Response(json.dumps(resp, 2), status=201, mimetype='application/json')
|
return Response(json.dumps(resp), status=201, mimetype='application/json')
|
||||||
|
|
||||||
|
|
||||||
# An example of reading data from Firebase
|
# An example of reading data from Firebase
|
||||||
|
|
Loading…
Reference in a new issue