Ejemplo n.º 1
0
    def post(self):
        # Validate json body
        fields = 'email', 'password'
        json_payload = request.get_json(silent=True)
        if not json_payload or not validate_body(fields, json_payload):
            return {'error': f'Must specify: {fields}'}, 400

        # Get user
        auth_user = User.find_by_email(json_payload['email'])
        if not auth_user:
            return {'error': 'User does not exist'}, 401
        # Verify password
        if not auth_user.verify_password(json_payload['password']):
            return {'error': 'Incorrect password'}, 401

        # Create token for user

        return {
            'data': {
                'msg': 'successfully signed in',
                'user_id': auth_user.id,
                'access_token': create_access_token(identity=auth_user.id),
                'refresh_token': create_refresh_token(identity=auth_user.id),
            }
        }
Ejemplo n.º 2
0
    def post(self):
        # Validate json body
        fields = 'email', 'password', 'firstname', 'lastname'
        json_payload = request.get_json(silent=True)
        print(json_payload)
        if not json_payload or not validate_body(fields, json_payload):
            return {'error': f'Must specify: {fields}'}, 400
        if not validate_body_values(fields, json_payload):
            return {'error': f'Must specify values for {fields}'}, 400
        # Check if user with email already exists
        if User.find_by_email(json_payload['email']) is not None:
            return {'error': f'{json_payload["email"]} already exists'}, 401
        # Create user & save
        new_user = User(**json_payload)
        new_user.save()

        return {
            'data': {
                'msg': 'user was successfully created',
                'user_id': f'{new_user.id}',
                'access_token': create_access_token(identity=new_user.id),
                'refresh_token': create_refresh_token(identity=new_user.id),
            }
        }