Last active
February 7, 2018 19:37
-
-
Save aaronpk/099d037a79441974e085bb919ac9f1b7 to your computer and use it in GitHub Desktop.
IndieAuth Client Quickstart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="/login.php" method="post"> | |
<input type="url" name="url"> | |
<input type="submit" value="Log In"> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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