Skip to content

Authorization

Arun Prakash edited this page May 17, 2024 · 12 revisions

πŸŽ– WordPress Authorization Guide

Working with WordPress often requires proper authorization, especially for tasks like creating, updating, and deleting content. Sometimes, to get more detailed information from the API, you need to set the context of list requests to 'edit', which also needs authorization, with the default being the 'view' context.

The wordpress_client makes authorization simple yet powerful. It also lets developers create their custom authorization methods by extending the base class.

🎨 Built-in Authorization Methods

wordpress_client comes with three main authorization methods:

You can easily use these methods by passing the respective class with every API request or setting it as the default during the client's setup.

🎨 Craft Your Custom Authorization

Want to create your own authorization style? It's simple!

  1. Create your CustomAuth class by extending the IAuthorization interface and adding the necessary functions and properties.
import 'package:wordpress_client/wordpress_client.dart';

class CustomAuth extends IAuthorization {
  CustomAuth({
    required this.userName,
    required this.password,
    super.events,
  });

  final String userName;
  final String password;

  @override
  bool get isValidAuth => throw UnimplementedError();

  @override
  String get scheme => throw UnimplementedError();

  @override
  Future<bool> authorize() {
    throw UnimplementedError();
  }

  @override
  Future<String?> generateAuthUrl() {
    throw UnimplementedError();
  }

  @override
  Future<bool> isAuthenticated() {
    throw UnimplementedError();
  }

  @override
  Future<bool> validate() {
    throw UnimplementedError();
  }

  @override
  void clientFactoryProvider(Dio client) {
    throw UnimplementedError();
  }
}
  1. Use this custom authorization for regular requests by passing the CustomAuth object with the request.
final auth = CustomAuth(
  userName: 'username',
  password: 'password',
);

final response = await client.media.list(
  ListMediaRequest(
    authorization: auth,
  ),
);
  1. For all requests from a single WordpressClient instance, set it as the default during client setup.
final client = WordpressClient(
  baseUrl: baseUrl,
  bootstrapper: (bootstrapper) => bootstrapper
      .withDefaultAuthorization(auth)
      .build(),
);

πŸ“Œ Things to Remember:

  • When using IAuthorization, you get a ready-to-use Dio network client with the Base URL through the clientFactoryProvider(...) function. Remember to store this client locally for subsequent requests.

  • 🌐 We're continuously improving this process in future releases.