Esempio n. 1
0
    def post(self):

        json_data = request.get_json()

        username = json_data.get('username')
        email = json_data.get('email')
        non_hash_password = json_data.get('password')

        if Admin.get_by_username(username):
            return {'message': 'username already used'}, HTTPStatus.BAD_REQUEST

        if Admin.get_by_email(email):
            return {'message': 'email already used'}, HTTPStatus.BAD_REQUEST

        password = hash_password(non_hash_password)

        admin = Admin(username=username, email=email, password=password)

        admin.save()

        data = {
            'id': admin.id,
            'username': admin.username,
            'email': admin.email,
            'password': admin.password
        }

        return data, HTTPStatus.OK
Esempio n. 2
0
    def post(self):

        json_data = request.get_json()

        email = json_data.get('email')
        password = json_data.get('password')

        admin = Admin.get_by_email(email=email)

        if not admin or not check_password(password, admin.password):
            return {'message': 'username or password is incorrect'}, HTTPStatus.UNAUTHORIZED

        access_token = create_access_token(identity=admin.id, fresh=True)
        refresh_token = create_refresh_token(identity=admin.id)

        return {'access_token': access_token, 'refresh_token': refresh_token}, HTTPStatus.OK