def post_auth_facebookregister(): invalid_form = False registration_error = '' accessToken = request.json.get('accessToken') payload = { 'fields': 'id,name,email', 'pretty': 0, 'suppress_http_code': 1, 'access_token': accessToken } response = requests.get('https://graph.facebook.com/v5.0/me', params=payload) json_data = json.loads(response.text) if 'id' not in json_data: return jsonify({ 'status': 'error', 'message': 'Error trying to register you in with your facebook account.' }), 400 provider_id = 'facebook$' + json_data['id'] if 'email' not in json_data or 'name' not in json_data: registration_error = "We're sorry your Facebook account is not associated with an email address. We cannot register you with Facebook. Please sign up for a SizeSquirrel account by filling out the Register form below and hitting the 'Register' button." return jsonify({'status': 'error', 'message': registration_error}), 400 else: username = json_data['email'].split('@')[0] email = json_data['email'] name = json_data['name'] user = User.query.filter_by(provider_id=provider_id).first() # register if not user: if User.query.filter(func.lower(User.username) == username.lower()).first() is not None: registration_error = "Username already exists." invalid_form = True if User.query.filter_by(email=email).first() is not None: registration_error = "A user with this email already exists." invalid_form = True if invalid_form is True: return jsonify({ 'status': 'error', 'message': registration_error }), 400 user = User(provider_id=provider_id, username=username, email=email, name=name, date_created=datetime.datetime.now(), date_last_login=datetime.datetime.now()) db.session.add(user) db.session.commit() token = user.get_token() return jsonify({ 'status': 'success', 'username': user.username, 'token': token, }) # user already exists if user: user.date_last_login = datetime.datetime.now() db.session.commit() token = user.get_token() return jsonify({ 'status': 'success', 'username': user.username, 'token': token, })
def post_auth_register(): invalid_form = False registration_errors = {} registration_errors['email'] = '' registration_errors['username'] = '' registration_errors['password'] = '' registration_errors['firstName'] = '' registration_errors['lastName'] = '' username = request.json.get('username') password = request.json.get('password') confirmPassword = request.json.get('confirmPassword') email = request.json.get('email') firstName = request.json.get('firstName') lastName = request.json.get('lastName') provider_id = 'sizesquirrel$' + email if not re.match(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$", email): registration_errors["email"] = "Invalid email address." invalid_form = True if not re.match(r"^[a-zA-Z0-9]+$", username): registration_errors[ "username"] = "******" invalid_form = True if len(username) < 3: registration_errors[ "username"] = "******" invalid_form = True if confirmPassword != password: registration_errors[ "password"] = "******" invalid_form = True if ' ' in password: registration_errors[ "password"] = "******" invalid_form = True if len(password) < 6: registration_errors[ "password"] = "******" invalid_form = True if username == "" or username is None: registration_errors["username"] = "******" invalid_form = True if password == "" or password is None: registration_errors["password"] = "******" invalid_form = True if email == "" or email is None: registration_errors["email"] = "This field is required." invalid_form = True if firstName == "" or firstName is None: registration_errors["firstName"] = "This field is required." invalid_form = True if lastName == "" or lastName is None: registration_errors["lastName"] = "This field is required." invalid_form = True if User.query.filter( func.lower(User.username) == username.lower()).first() is not None: registration_errors["username"] = "******" invalid_form = True if User.query.filter_by(email=email).first() is not None: registration_errors["email"] = "A user with this email already exists." invalid_form = True if invalid_form is True: return jsonify({ 'status': 'error', 'message': registration_errors }), 400 else: name = firstName + ' ' + lastName user = User(username=username, email=email, provider_id=provider_id, name=name, date_created=datetime.datetime.now(), date_last_login=datetime.datetime.now()) user.hash_password(password) db.session.add(user) db.session.commit() token = user.get_token() return jsonify({ 'status': 'success', 'username': user.username, 'token': token, })