コード例 #1
0
    def post(self):
        data = parser.parse_args(strict=True)
        # check if user exists already
        if UserModel.find_by_username(data['username']):
            return {
                'message': 'User {} already exists'.format(data['username'])
            }
        # create a new user
        new_user = UserModel(username=data['username'],
                             password=UserModel.generate_hash(
                                 data['password']))
        # attempt saving user to the DB
        try:
            new_user.save_to_db()
            # create temporary access token
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])

            return {
                'message': 'User {} was created'.format(data['username']),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        except:
            return {
                'message': 'Something went wrong while creating the user'
            }, 500