mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-22 21:45:48 +00:00
Merge pull request #3 from LEG-Industries/user-verification
User verification
This commit is contained in:
commit
4d11eafbed
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,6 +1,5 @@
|
|||
import json
|
||||
import lib.mail
|
||||
from random import randint
|
||||
import lib.utils
|
||||
from flask import Response, Blueprint, request
|
||||
from firebase_admin import firestore, auth
|
||||
from firebase_admin._auth_utils import EmailAlreadyExistsError
|
||||
|
@ -33,19 +32,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 +44,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,8 +0,0 @@
|
|||
from flask import current_app
|
||||
from flask_mail import Mail, Message
|
||||
|
||||
def send(subject, sender, recipients, body):
|
||||
mail = Mail(current_app)
|
||||
msg = Message(subject, sender=sender, recipients=recipients)
|
||||
msg.body = body
|
||||
mail.send(msg)
|
33
lib/utils.py
Normal file
33
lib/utils.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
from random import randint
|
||||
from flask import current_app
|
||||
from flask_mail import Mail, Message
|
||||
from firebase_admin import auth, 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)
|
||||
|
||||
def userLoggedInAndVerfied(token):
|
||||
# Need frontend to test this
|
||||
# decoded_token = auth.verify_id_token(token)
|
||||
# uid = decoded_token['uid']
|
||||
# isVerified = auth.get_user(uid).email_verified
|
||||
return True #placeholder
|
|
@ -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
3
requirements.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
flask
|
||||
flask_mail
|
||||
firebase_admin
|
Loading…
Reference in a new issue