def update_email(user_id): req = request.get_json() utils.check_params(req, 'email', 'password', 'new_email') if req['email'] == req['new_email']: return jsonify( {'message': 'Your email is already ' + req['new_email']}) user = Users.query.filter_by(id=user_id, email=req['email'], password=utils.sha256( req['password'])).first() if user is None: raise APIException('User not found', 404) user.status = 'invalid' user.email = req['new_email'] db.session.commit() send_email(template='email_validation', emails=user.email, data={ 'validation_link': utils.jwt_link(user.id, role='email_change') }) return jsonify({'message': 'Please verify your new email'}), 200
def register_user(): req = request.get_json() check_params(req, 'email', 'password') email = req['email'].strip() regex = r'^[a-zA-Z]+[\w\.]*@\w+\.[a-zA-Z]{2,5}$' if re.search(regex, email, re.IGNORECASE) is None: raise APIException('This is not a valid email', 401) if len( req['password'] ) < 6: raise APIException('Password must be at least 6 characters long', 401) # If user exists and failed to validate his account # user = (Users.query # .filter_by( email=email, password=sha256(req['password']) ) # .first()) user = (Users.query .filter_by( email=email ) .first()) print('user', user) if user and user.status._value_ == 'invalid': data = {'validation_link': jwt_link(user.id)} send_email( template='email_validation', emails=user.email, data=data) return jsonify({'message':'Another email has been sent for email validation'}) elif user and user.status._value_ == 'valid': print('should be ehreCCC') raise APIException('This email address is already taken', 400) print('should be ehreA') user = Users( email = email, password = sha256(req['password']) ) print('should be ehreBBBB') db.session.add(user) db.session.commit() send_email( template='email_validation', emails=user.email, data={'validation_link': jwt_link(user.id)} ) return jsonify({'message': 'Please verify your email'}), 200
def reset_password(): req = request.get_json() utils.check_params(req, 'email') # User forgot their password if request.args.get('forgot') == 'true': user = Users.query.filter_by(email=req['email']).first() if user is None: raise APIException('This email is not registered', 400) send_email('reset_password_link', emails=req['email'], data={ 'link': utils.jwt_link(user.id, 'users/reset_password/', req['email']) }) return jsonify({ 'message': 'A link has been sent to your email to reset the password' }), 200 # User knows their password utils.check_params(req, 'password', 'new_password') if req['password'] == req['new_password']: raise APIException( 'Your new password is the same as the old password') if len(req['new_password']) < 6: raise APIException( 'Your new password must be at least 6 characters long') user = Users.query.filter_by(email=req['email'], password=utils.sha256( req['password'])).first() if user is None: raise APIException('User not found', 404) user.password = utils.sha256(req['new_password']) db.session.commit() return jsonify({'message': 'Your password has been changed'}), 200