diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..603f0b6 --- /dev/null +++ b/.gcloudignore @@ -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 \ No newline at end of file diff --git a/api/authentication.py b/api/authentication.py index 613ed59..8c56b95 100644 --- a/api/authentication.py +++ b/api/authentication.py @@ -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') \ No newline at end of file + 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') + diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..a0b5f22 --- /dev/null +++ b/app.yaml @@ -0,0 +1 @@ +runtime: python38 \ No newline at end of file diff --git a/lib/utils.py b/lib/utils.py new file mode 100644 index 0000000..2488e41 --- /dev/null +++ b/lib/utils.py @@ -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) diff --git a/app.py b/main.py similarity index 93% rename from app.py rename to main.py index 3196e5e..e12d427 100644 --- a/app.py +++ b/main.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9af04b9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +flask_mail +firebase_admin \ No newline at end of file