mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-11-10 01:35:50 +00:00
Added verification endpoint
This commit is contained in:
parent
0bb3514dc3
commit
8b1ce01079
19
.gcloudignore
Normal file
19
.gcloudignore
Normal 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
|
|
@ -1,5 +1,5 @@
|
||||||
import json
|
import json
|
||||||
import lib.mail
|
import lib.utils
|
||||||
from random import randint
|
from random import randint
|
||||||
from flask import Response, Blueprint, request
|
from flask import Response, Blueprint, request
|
||||||
from firebase_admin import firestore, auth
|
from firebase_admin import firestore, auth
|
||||||
|
@ -33,19 +33,9 @@ def register():
|
||||||
except EmailAlreadyExistsError:
|
except EmailAlreadyExistsError:
|
||||||
return Response("{'error':'User with given email address already exists'}", status=409, mimetype='application/json')
|
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 get verified
|
||||||
code = randint(100000, 999999)
|
code = lib.utils.saveVerificationCode(user.uid)
|
||||||
data = {
|
lib.utils.sendVerificationMail(name, email, code)
|
||||||
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)
|
|
||||||
|
|
||||||
# Link the user to the device
|
# Link the user to the device
|
||||||
data = {
|
data = {
|
||||||
|
@ -55,4 +45,28 @@ def register():
|
||||||
|
|
||||||
# User successfully created and linked to device, return 201
|
# User successfully created and linked to device, return 201
|
||||||
resp = {"uid": user.uid}
|
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')
|
||||||
|
|
||||||
|
|
26
lib/utils.py
Normal file
26
lib/utils.py
Normal 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)
|
|
@ -18,5 +18,9 @@ app.config['MAIL_USE_SSL'] = MAIL_USE_SSL
|
||||||
# Initialize Firebase
|
# Initialize Firebase
|
||||||
firebase = initialize_app(credentials.Certificate('firebase-key.json'))
|
firebase = initialize_app(credentials.Certificate('firebase-key.json'))
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def hello():
|
||||||
|
return 'Hello World'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run()
|
app.run()
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
flask
|
||||||
|
flask_mail
|
||||||
|
firebase_admin
|
Loading…
Reference in a new issue