Skip to content

Commit 228b69b

Browse files
committed
Improved basic example and documentation
1 parent 06f5a80 commit 228b69b

File tree

2 files changed

+73
-35
lines changed

2 files changed

+73
-35
lines changed

README.md

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Auth0 PHP SDK
22

3-
## Installation
3+
### 1. Install the SDK
44

5-
Using [Composer](http://getcomposer.org/doc/01-basic-usage.md):
5+
We recommend using [Composer](http://getcomposer.org/doc/01-basic-usage.md) to install the library.
6+
7+
Modify your `composer.json` to add the following dependencies and run `composer update`.
68

79
~~~js
810
{
@@ -13,24 +15,10 @@ Using [Composer](http://getcomposer.org/doc/01-basic-usage.md):
1315
}
1416
~~~
1517

16-
## Usage
17-
18-
1- Include the [Auth0 Widget](https://docs.auth0.com/login-widget2):
18+
### 2. Setup the callback action
19+
Create a php page (or action if you are using an MVC framework) that will handle the callback from the login attempt.
1920

20-
~~~html
21-
<a href="javascript:widget.signin();">Login</a>
22-
23-
<script src="https://d19p4zemcycm7a.cloudfront.net/w2/auth0-widget-2.4.min.js"></script>
24-
<script type="text/javascript">
25-
var widget = new Auth0Widget({
26-
domain: 'YOUR_AUTH0_DOMAIN',
27-
clientID: 'YOUR_AUTH0_CLIENT_ID',
28-
callbackURL: 'YOUR_AUTH0_APP_CALLBACK'
29-
});
30-
</script>
31-
~~~
32-
33-
2- Edit the callback page to create an instance of the `Auth0` class in order to exchange the authorization code (provided by Auth0) for an access_token:
21+
In there, you should create an instance of the SDK with the proper configuration and ask for the user information.
3422

3523
~~~php
3624
use Auth0SDK\Auth0;
@@ -39,19 +27,69 @@ $auth0 = new Auth0(array(
3927
'domain' => 'YOUR_AUTH0_DOMAIN',
4028
'client_id' => 'YOUR_AUTH0_CLIENT_ID',
4129
'client_secret' => 'YOUR_AUTH0_CLIENT_SECRET',
42-
'redirect_uri' => 'YOUR_AUTH0_APP_CALLBACK',
43-
'debug' => true
30+
'redirect_uri' => 'http://<name>/callback.php'
4431
));
4532

46-
$access_token = $auth0->getAccessToken();
33+
$userInfo = $auth0->getUserInfo();
4734
~~~
4835

49-
3- Once the user successfully authenticated to the application, you can retrieve his profile:
36+
If the user was already logged in, `getUserInfo()` will retrieve that [user information](https://docs.auth0.com/user-profile) from the `PHP Session`. If not, it will try to exchange the code given to the callback to get an access token, id token and the [user information](https://docs.auth0.com/user-profile) from auth0.
37+
38+
This makes it possible to use the same code in the callback action and any other page, so to see if there is a logged in user, you can call
39+
5040

5141
~~~php
52-
$userProfile = $auth0->getUserInfo();
42+
// ...
43+
// code from above
44+
45+
if (!$userInfo) {
46+
// print login button
47+
} else {
48+
// Say hello to $userInfo['name']
49+
// print logout button
50+
}
5351
~~~
5452

53+
### 3. Setup the callback action in Auth0
54+
55+
After authenticating the user on Auth0, we will do a GET to a URL on your web site. For security purposes, you have to register this URL on the Application Settings section on Auth0 Admin app.
56+
57+
http://<name>/callback.php
58+
59+
60+
### 4. Triggering login manually or integrating the Auth0 widget
61+
62+
You can trigger the login in different ways, like redirecting to a login link or using the [Login Widget](https://docs.auth0.com/login-widget2), by adding the following javascript into your page
63+
64+
65+
<a href="javascript:widget.signin();">Login</a>
66+
67+
<script src="https://cdn.auth0.com/w2/auth0-widget-3.0.min.js"></script>
68+
<script type="text/javascript">
69+
var widget = new Auth0Widget({
70+
domain: 'YOUR_AUTH0_DOMAIN',
71+
clientID: 'YOUR_AUTH0_CLIENT_ID',
72+
callbackURL: 'http://<name>/callback.php'
73+
});
74+
</script>
75+
76+
77+
### 5. (Optional) Configure session data
78+
79+
By default, the SDK will store the [user information](https://docs.auth0.com/user-profile) in the `PHP Session` and it will discard the access token and the id token. If you like to persist them as well, you can pass `persist_access_token => true` and `persist_id_token => true` to the SDK configuration in step 2. You can also disable session all together by passing `store => false`.
80+
81+
If you want to change `PHP Session` and use Laravel, Zend, Symfony or other abstraction to the session, you can create a class that implements `get`, `set`, `delete` and pass it to the SDK as following.
82+
83+
~~~php
84+
$laravelStore = new MyLaravelStore();
85+
$auth0 = new Auth0(array(
86+
// ...
87+
'store' => $laravelStore,
88+
// ...
89+
));
90+
~~~
91+
92+
5593
## Develop
5694

5795
This SDK uses [Composer](http://getcomposer.org/doc/01-basic-usage.md) to manage its dependencies.

examples/basic/index.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'redirect_uri' => $auth0_cfg['redirect_uri']
1212
));
1313

14-
$access_token = $auth0->getAccessToken();
14+
$userInfo = $auth0->getUserInfo();
1515

1616
?>
1717
<!doctype html>
@@ -21,18 +21,18 @@
2121
<title>Testing Auth0 PHP</title>
2222
</head>
2323
<body>
24-
<?php if(!$access_token): ?>
24+
<?php if(!$userInfo): ?>
2525
<script src="https://d19p4zemcycm7a.cloudfront.net/w2/auth0-widget-2.3.min.js"></script>
26-
<script type="text/javascript">
27-
var widget = new Auth0Widget({
28-
domain: "<?php echo $auth0->getDomain() ?>",
29-
clientID: "<?php echo $auth0->getClientId() ?>",
30-
callbackURL: "<?php echo $auth0->getRedirectUri() ?>"
31-
});
32-
</script>
33-
<button onclick="widget.signin()">Login</button>
26+
<script type="text/javascript">
27+
var widget = new Auth0Widget({
28+
domain: "<?php echo $auth0->getDomain() ?>",
29+
clientID: "<?php echo $auth0->getClientId() ?>",
30+
callbackURL: "<?php echo $auth0->getRedirectUri() ?>"
31+
});
32+
</script>
33+
<button onclick="widget.signin()">Login</button>
3434
<?php else: ?>
35-
<?php var_dump($auth0->getUserInfo()) ?>
35+
<?php var_dump($userInfo) ?>
3636
<?php endif ?>
3737
</body>
3838
</html>

0 commit comments

Comments
 (0)