You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to get started quickly, and if you're okay with letting the library store things in the PHP session itself, then you can follow the examples below. If you need more control or want to step into the details of the IndieAuth flow, see the [Detailed Usage for Clients](#detailed) below.
13
+
14
+
### Create a Login Form
15
+
16
+
You'll first need to create a login form to prompt the user to enter their website address. This might look something like the HTML below.
17
+
18
+
```html
19
+
<formaction="/login.php"method="post">
20
+
<inputtype="url"name="url">
21
+
<inputtype="submit"value="Log In">
22
+
</form>
23
+
```
24
+
25
+
### Begin the Login Flow
26
+
27
+
In the `login.php` file, you'll need to initialize the session, and tell this library to discover the user's endpoints. If everything succeeds, the library will return a URL that you can use to redirect the user to begin the flow.
28
+
29
+
The example below will have some really basic error handling, which you'll probably want to replace with something nicer looking.
30
+
31
+
Example `login.php` file:
32
+
33
+
```php
34
+
<?php
35
+
36
+
if(!isset($_POST['url'])) {
37
+
die('Missing URL');
38
+
}
39
+
40
+
// Start a session for the library to be able to save state between requests.
41
+
session_start();
42
+
43
+
// You'll need to set up two pieces of information before you can use the client,
44
+
// the client ID and and the redirect URL.
45
+
46
+
// The client ID should be the home page of your app.
// or list($authorizationURL, $error) = IndieAuth\Client::begin($_POST['url']);
58
+
59
+
// Check whether the library was able to discover the necessary endpoints
60
+
if($error) {
61
+
echo "<p>Error: ".$error['error']."</p>";
62
+
echo "<p>".$error['error_description']."</p>";
63
+
} else {
64
+
// Redirect the user to their authorization endpoint
65
+
header('Location: '.$authorizationURL);
66
+
}
67
+
68
+
```
69
+
70
+
### Handling the Callback
71
+
72
+
In your callback file, you just need to pass all the query string parameters to the library and it will take care of things! It will use the authorization or token endpoint it found in the initial step, and will check the authorization code or exchange it for an access token as appropriate.
73
+
74
+
The result will be the response from the authorization endpoint, which will contain the user's final `me` URL as well as the access token if you requested one or more scopes.
75
+
76
+
If there were any problems, the error information will be returned to you as well.
77
+
78
+
The library takes care of canonicalizing the user's URL, as well as checking that the final URL is on the same domain as the entered URL.
// You'll probably want to save the user's URL in the session
104
+
$_SESSION['user'] = $user['me'];
105
+
}
106
+
107
+
```
108
+
109
+
110
+
Detailed Usage for Clients {#detailed}
111
+
--------------------------
10
112
11
113
The first thing an IndieAuth client needs to do is to prompt the user to enter their web address. This is the basis of IndieAuth, requiring each person to have their own website. A typical IndieAuth sign-in form may look something like the following.
// Unfortunately I've seen a bunch of websites return different content when the user agent is set to something like curl or other server-side libraries, so we have to pretend to be a browser to successfully get the real HTML
0 commit comments