Exemplo n.º 1
0
    def post(self):
        data = load_json()

        # get the data
        try:
            email = data['email']
            password = data['password']
        except KeyError:
            return {'message': 'email and password are required'}, 422

        # choose whether the email and password match or not
        validated, user, code = validate_user(email, password)

        if validated:
            give_token = True
            output = {'status': 'success', 'loggedIn': True}
        else:
            give_token = False
            user['loggedIn'] = False
            output = user

        # add a token to the output (if applicable)
        if give_token:
            token = jwt.encode(
                {
                    'id': user.id,
                    'exp': dt.utcnow() + timedelta(minutes=TOKEN_MINUTES)
                }, app.config.get('SECRET_KEY'))
            output['token'] = token.decode('UTF-8')

        return output, code
Exemplo n.º 2
0
    def delete(self):
        json_data = load_json()

        # get the data
        try:
            email = json_data['email']
            password = json_data['password']
        except KeyError:
            return {'message': 'email and password are required'}, 422

        validated, user, code = validate_user(email, password)

        if not validated:
            return user, code

        # delete the user
        db.session.delete(user)
        db.session.commit()

        return {'status': 'success', 'message': f"Deleted account attached to {user.email}"}, 201
Exemplo n.º 3
0
    def put(self):
        json_data = load_json()

        # get relevant data
        try:
            email = json_data['email']
            old_password = json_data['old_password']
            new_password = json_data['new_password']
        except KeyError:
            return {'message', 'email, old_password, and new_password are required'}, 422

        validated, user, code = validate_user(email, old_password)

        if not validated:
            return user, code

        # change the password
        user.password = bcrypt.generate_password_hash(new_password).decode('utf-8')
        db.session.commit()

        return {'status': 'success'}, 201