@@ -13,27 +13,40 @@ With the following PHP code below, you can display a log in button that, when cl
1313``` php
1414<?php
1515use Authwave\Authenticator;
16+ use Authwave\Token;
1617require __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 .
2021define("CLIENT_KEY", "1234567890abcdef");
2122define("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:
3237if(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}
3545elseif(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