def post(self): post_data = request.get_json() # Query to see if the user already exists user = UserModel.find_by_email(email=post_data.get('email', None)) # If user doesn't already exist, create a user if not user: try: # Register the user email = post_data['email'] plaintext_password = post_data['password'] user = UserModel(email, plaintext_password) user.save_to_db() response = { 'message': 'You registered successfully. Please login.', } return make_response(jsonify(response)), HTTP_201_CREATED except Exception as e: response = {'message': str(e)} return make_response(jsonify(response)), HTTP_401_UNAUTHORIZED else: response = {'message': 'User already exists. Please login.'} return make_response(jsonify(response)), HTTP_202_ACCEPTED
def delete(self, pk): item = UserModel.find_by_id(pk) if item: item.delete_from_db() return {'message': 'User has been deleted'}
def init_database(): # Create the database and the database table db.create_all() # Insert user data user1 = UserModel('*****@*****.**', 'password1') user2 = UserModel('*****@*****.**', 'password2') user2 = UserModel('*****@*****.**', 'password') db.session.add(user1) db.session.add(user2) # Commit the changes for the users db.session.commit() yield db # This is where the testing happens! db.drop_all()
def verify_token(token): """ Verify token and return user decoded from payload. """ s = Serializer(config.SECRET_KEY) try: data = s.loads(token) except: return None id = data.get('user') if id: return UserModel.find_by_id(id) return None
def post(self): # Get email from json payload post_data = request.get_json() email = post_data.get('email', None) user = UserModel.find_by_email(email) # If the user exists, send them email # with secure token if user: # Create encrypted user token token = create_token(user) # Send email to user pass
def get(self, pk: int): user = UserModel.find_by_id(pk) if user: return user.as_dict(), HTTP_200_OK return {'message': 'User not found'}, HTTP_404_NOT_FOUND
def get(self, pk): user = UserModel.find_by_id(pk) if user: return user.json() return {'message': 'User not found'}, 404
def authenticate(email, password): user = UserModel.find_by_email(email) if user and user.is_correct_password(password): return user else: return None
def jwt_identity(payload): return UserModel.get_by_id(payload)
def new_user(): user = UserModel('*****@*****.**', 'some_password') return user