Skip to content

Commit 3477e05

Browse files
author
Greg Bowler
committed
Update usage example with Token
1 parent 8110cd5 commit 3477e05

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,40 @@ With the following PHP code below, you can display a log in button that, when cl
1313
```php
1414
<?php
1515
use Authwave\Authenticator;
16+
use Authwave\Token;
1617
require __DIR__ . "/vendor/autoload.php";
1718

1819
// These constants can be loaded from your application's configuration
19-
// or environment variables.
20+
// or environment variables, and must be created within Authwave.
2021
define("CLIENT_KEY", "1234567890abcdef");
2122
define("CLIENT_SECRET", "aaaa-bbbb-cccc-dddd-eeee-ffff");
2223

24+
// Persist a Token in the session to handle the remote authentication flow.
25+
$token = $_SESSION["authwave-token"] ?? new Token(CLIENT_KEY, CLIENT_SECRET);
26+
$_SESSION["authwave-token"] = $token;
27+
2328
// Construct the Authenticator class as soon as possible, as this handles the
2429
// Authentication steps passed via the query string from Authwave.
2530
$auth = new Authenticator(
26-
$_SERVER["REQUEST_URI"],
27-
CLIENT_KEY,
28-
CLIENT_SECRET
31+
$token,
32+
"example.com",
33+
$_SERVER["REQUEST_URI"]
2934
);
3035

3136
// Handle authentication login/logout action via the querystring:
3237
if(isset($_GET["login"])) {
33-
$auth->login();
38+
// Redirect the user agent to the auth uri, which is a location on the remote
39+
// provider. The remote provider will in turn redirect the user agent back to
40+
// the return URI (set as 3rd parameter of Authenticator's constructor), at
41+
// which point the user will be considered authenticated.
42+
header("Location: " . $auth->getAuthUri(), true, 303);
43+
exit;
3444
}
3545
elseif(isset($_GET["logout"])) {
36-
$auth->logout();
46+
// To log out, simply remove the Token from the session and reload the page.
47+
unset($_SESSION["authwave-token"]);
48+
header("Location: " . $_SERVER["REQUEST_URI"]);
49+
exit;
3750
}
3851

3952
// Authentication is handled by Authwave, so you can trust "isLoggedIn"

0 commit comments

Comments
 (0)