-
Notifications
You must be signed in to change notification settings - Fork 14
Authorization
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.
wordpress_client comes with three main authorization methods:
-
AppPasswordAuth
: π App Password Auth by The WordPress Team -
BasicJwtAuth
: π Basic JWT Authentication by Enrique Chavez (Note: No longer under active development) -
UsefulJwtAuth
: π Useful JWT Authentication by Useful Team (Highly Recommended!)
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.
Want to create your own authorization style? It's simple!
- Create your
CustomAuth
class by extending theIAuthorization
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();
}
}
- 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,
),
);
- 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 theclientFactoryProvider(...)
function. Remember to store this client locally for subsequent requests. -
π We're continuously improving this process in future releases.