Merge pull request #3 from LEG-Industries/user-verification

User verification
This commit is contained in:
Benjamin Ramhorst 2022-02-07 13:16:52 +00:00 committed by GitHub
commit 4d11eafbed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 24 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,6 +1,5 @@
import json import json
import lib.mail import lib.utils
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
from firebase_admin._auth_utils import EmailAlreadyExistsError from firebase_admin._auth_utils import EmailAlreadyExistsError
@ -33,19 +32,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 = {
@ -56,3 +45,27 @@ 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')

1
app.yaml Normal file
View file

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

View file

@ -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
View 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

View file

@ -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
View file

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