def register(): post_data = request.get_json() email = post_data.get('email') password = post_data.get('password') if email is None or password is None: responseObject = {'status': 'error', 'message': 'Invalid input.'} return jsonify(responseObject), 400 if Users.query.filter_by(email=email).first() is not None: responseObject = {'status': 'error', 'message': 'User already exists.'} return jsonify(responseObject), 400 user = Users(first_name=post_data.get('first_name'), last_name=post_data.get('last_name'), email=post_data.get('email'), password=post_data.get('password'), age=post_data.get('age'), first_day=post_data.get('first_day'), cycle_length=post_data.get('cycle_length'), non_hormonal=post_data.get('non_hormonal'), triphasic=post_data.get('triphasic'), monophasic=post_data.get('monophasic'), progestin=post_data.get('progestin')) db.session.add(user) db.session.commit() auth_token = user.encode_auth_token(user.id) responseObject = { 'status': 'success', 'message': 'Successfully registered', 'auth_token': auth_token.decode() } return jsonify(responseObject), 201
def register(): post_data = request.get_json() email = post_data.get('email') password = post_data.get('password') if email is None or password is None: responseObject = {'status': 'error', 'message': 'Invalid input.'} return jsonify(responseObject), 400 if Users.query.filter_by(email=email).first() is not None: responseObject = {'status': 'error', 'message': 'User already exists.'} return jsonify(responseObject), 400 user = Users(first_name=post_data.get('first_name'), last_name=post_data.get('last_name'), email=post_data.get('email'), password=post_data.get('password'), skill_level=post_data.get('skill_level'), instrument=post_data.get('instrument'), instructor=post_data.get('instructor'), bio=post_data.get('bio'), avatar=post_data.get('avatar'), phone_number=post_data.get('phone_number')) db.session.add(user) db.session.commit() auth_token = user.encode_auth_token(user.id) responseObject = { 'status': 'success', 'message': 'Successfully registered', 'auth_token': auth_token.decode(), 'instructor': user.instructor, 'id': user.id } return jsonify(responseObject), 201
def login() -> wrappers.Response: auth = request.authorization if not auth or not auth.username or not auth.password: return make_response( 'Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required!"'}) user = Users.query.filter_by(name=auth.username).first() if not user: return make_response( 'Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required!"'}) if check_password_hash(user.password, auth.password): return jsonify({'token': Users.encode_auth_token(user)}), 200 return make_response('Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required!"'})
def test_encode_auth_token(self): user = Users(id=1, name='test_admin', password='******', admin=True) auth_token = user.encode_auth_token(user) self.assertTrue(isinstance(auth_token, str))