Skip to content

Instantly share code, notes, and snippets.

@aaronpk

aaronpk/index.php

Last active Feb 7, 2018
Embed
What would you like to do?
IndieAuth Client Quickstart
<form action="/login.php" method="post">
<input type="url" name="url">
<input type="submit" value="Log In">
</form>
<?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/';
// The redirect URL is where the user will be returned to after they approve the request.
IndieAuth\Client::$redirectURL = 'https://example.com/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);
}
<?php
require('vendor/autoload.php');
session_start();
IndieAuth\Client::$clientID = 'https://example.com/';
IndieAuth\Client::$redirectURL = 'https://example.com/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>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment