Added verification endpoint

This commit is contained in:
Benjamin Ramhorst 2022-02-07 13:10:58 +00:00
parent 0bb3514dc3
commit 8b1ce01079
6 changed files with 82 additions and 15 deletions

19
.gcloudignore Normal file
View file

@ -0,0 +1,19 @@
# This file specifies files that are *not* uploaded to Google Cloud
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore
# Python pycache:
__pycache__/
# Ignored by the build system
/setup.cfg

View file

@ -1,5 +1,5 @@
import json
import lib.mail
import lib.utils
from random import randint
from flask import Response, Blueprint, request
from firebase_admin import firestore, auth
@ -33,19 +33,9 @@ def register():
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)
subject = 'Please verify your email for BarkFinder'
sender = 'legbarkr@gmail.com'
recipients = [email]
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)
lib.mail.send(subject, sender, recipients, body)
# Prompt the user to get verified
code = lib.utils.saveVerificationCode(user.uid)
lib.utils.sendVerificationMail(name, email, code)
# Link the user to the device
data = {
@ -55,4 +45,28 @@ def register():
# User successfully created and linked to device, return 201
resp = {"uid": user.uid}
return Response(json.dumps(resp), status=201, mimetype='application/json')
return Response(json.dumps(resp), status=201, mimetype='application/json')
@authentication.route('/authentication/verify', methods=['POST'])
def verify():
body = request.json
if body is None:
return Response("{'error':'Invalid request - please provide a body'}", status=400, mimetype='application/json')
uid = body['uid']
code = body['code']
doc = firestore.client().collection(u'verification').document(uid).get()
if doc.exists:
if doc.to_dict()['code'] == code:
auth.update_user(uid, email_verified=True)
firestore.client().collection(u'verification').document(uid).delete()
return Response("{'error':'User verified'}", status=200, mimetype='application/json')
else:
return Response("{'error':'Invalid code'}", status=400, mimetype='application/json')
else:
user = auth.get_user(uid)
code = lib.utils.saveVerificationCode(user.uid)
lib.utils.sendVerificationMail(user.display_name, user.email, code)
return Response("{'error':'Server couldn't find code, creating new one and sending email'}", status=500, mimetype='application/json')

1
app.yaml Normal file
View file

@ -0,0 +1 @@
runtime: python38

26
lib/utils.py Normal file
View file

@ -0,0 +1,26 @@
from random import randint
from flask import current_app
from flask_mail import Mail, Message
from firebase_admin import firestore
def sendMail(subject, sender, recipients, body):
mail = Mail(current_app)
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = body
mail.send(msg)
def saveVerificationCode(uid):
code = randint(100000, 999999)
data = {
u'code': code
}
firestore.client().collection(u'verification').document(uid).set(data)
return code
def sendVerificationMail(name, email, code):
subject = 'Please verify your email for BarkFinder'
sender = 'legbarkr@gmail.com'
recipients = [email]
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)
sendMail(subject, sender, recipients, body)

View file

@ -18,5 +18,9 @@ app.config['MAIL_USE_SSL'] = MAIL_USE_SSL
# Initialize Firebase
firebase = initialize_app(credentials.Certificate('firebase-key.json'))
@app.route('/')
def hello():
return 'Hello World'
if __name__ == '__main__':
app.run()

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
flask
flask_mail
firebase_admin