This release includes two new methods for quickly developing an IndieAuth client.
The library can now handle all the boilerplate work of generating a state parameter, URL canonicalization, and state management between the request and callback.
Developing an IndieAuth client now requires just setting a few configuration variables and deciding how to show error messages in your application. See the code below for an example of using the new features!
index.php
<form action="/login.php" method="post">
<input type="url" name="url">
<input type="submit" value="Log In">
</form>
login.php
<?php
require('vendor/autoload.php');
if(!isset($_POST['url'])) {
die('Missing URL');
}
// Start a session for the library to be able to save state between requests.
session_start();
// You'll need to set up two pieces of information before you can use the client,
// the client ID and and the redirect URL.
// The client ID should be the home page of your app.
IndieAuth\Client::$clientID = 'https://example.com/client/';
// The redirect URL is where the user will be returned to after they approve the request.
IndieAuth\Client::$redirectURL = 'https://example.com/client/redirect.php';
// Pass the user's URL and your requested scope to the client.
// If you are writing a Micropub client, you should include at least the "create" scope.
// If you are just trying to log the user in, you can omit the second parameter.
list($authorizationURL, $error) = IndieAuth\Client::begin($_POST['url']);
// or list($authorizationURL, $error) = IndieAuth\Client::begin($_POST['url']);
// Check whether the library was able to discover the necessary endpoints
if($error) {
echo "<p>Error: ".$error['error']."</p>";
echo "<p>".$error['error_description']."</p>";
} else {
// Redirect the user to their authorization endpoint
header('Location: '.$authorizationURL);
}
redirect.php
<?php
require('vendor/autoload.php');
session_start();
IndieAuth\Client::$clientID = 'https://example.com/client/';
IndieAuth\Client::$redirectURL = 'https://example.com/client/redirect.php';
list($user, $error) = IndieAuth\Client::complete($_GET);
if($error) {
echo "<p>Error: ".$error['error']."</p>";
echo "<p>".$error['error_description']."</p>";
} else {
// Login succeeded!
// If you requested a scope, then there will be an access token in the response.
// Otherwise there will just be the user's URL.
echo "URL: ".$user['me']."<br>";
if(isset($user['access_token'])) {
echo "Access Token: ".$user['access_token']."<br>";
echo "Scope: ".$user['scope']."<br>";
}
}
@aaronpk great work Aaron. 👍