Skip to content

Commit

Permalink
refactor(console): fix php guide (#6143)
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun authored Jul 1, 2024
1 parent 4727062 commit 01558bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/console/src/assets/docs/fragments/_checkpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ Now, you can test your application:

1. Run your application, you will see the sign-in button.
2. Click the sign-in button, the SDK will init the sign-in process and redirect you to the Logto sign-in page.
3. After you signed in, you will be redirected back to your application and see user data and the sign-out button.
3. After you signed in, you will be redirected back to your application and see the sign-out button.
4. Click the sign-out button to sign out.
33 changes: 13 additions & 20 deletions packages/console/src/assets/docs/guides/web-php/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ composer require logto/sdk
Insert the following code into your PHP file:

<Code title="index.php" className="language-php">
{`use logto\sdk\LogtoClient;
use Logto\Sdk\LogtoConfig;
{`use Logto\\Sdk\\LogtoClient;
use Logto\\Sdk\\LogtoConfig;
$client = new LogtoClient(
new LogtoConfig(
Expand All @@ -53,7 +53,7 @@ By default, the SDK uses the built-in PHP session to store the Logto data. If yo

</Step>

<Step title="Implement the sign-in route">
<Step title="Configure redirect URIs">

<RedirectUrisWeb />

Expand Down Expand Up @@ -123,33 +123,18 @@ In Logto SDK, we can use `$client->isAuthenticated()` to check the authenticatio
We also need to implement a home page for demonstration:

- If the user is not signed in, show a sign-in button;
- If the user is signed in, show some basic information about the user.
- If the user is signed in, show a sign-out button.

```php title="index.php"
Route::get('/', function () {
if ($client->isAuthenticated() === false) {
return "Not authenticated <a href='/sign-in'>Sign in</a>";
}

return (
// Get local ID token claims
json_decode($client->getIdTokenClaims())
. "<br>"
// Fetch user info from Logto userinfo endpoint
json_decode($client->fetchUserInfo())
. "<br><a href='/sign-out'>Sign out</a>"
);
return "<a href='/sign-out'>Sign out</a>";
});
```

Our data models are based on [JsonModel](https://github.com/logto-io/php/blob/master/docs/api/classes/Logto/Sdk/Models/JsonModel.md), which is safe to accept undefined keys while encoding or decoding JSON.

Note that a field (claim) with `null` value doesn't mean the field is set. The reason may be the related scope is not requested, or the user doesn't have the field.

For example, if we didn't request the `email` scope when signing in, and the `email` field will be `null`. However, if we requested the `email` scope, the `email` field will be the user's email address if available.

To learn more about scopes and claims, see [Get user information](https://docs.logto.io/quick-starts/php/#get-user-information).

</Step>

<Step title="Checkpoint: Test your application">
Expand Down Expand Up @@ -179,6 +164,14 @@ Route::get('/userinfo', function () {
});
```

Our data models are based on [JsonModel](https://github.com/logto-io/php/blob/master/docs/api/classes/Logto/Sdk/Models/JsonModel.md), which is safe to accept undefined keys while encoding or decoding JSON.

Note that a field (claim) with `null` value doesn't mean the field is set. The reason may be the related scope is not requested, or the user doesn't have the field.

For example, if we didn't request the `email` scope when signing in, and the `email` field will be `null`. However, if we requested the `email` scope, the `email` field will be the user's email address if available.

To learn more about scopes and claims, see [Get user information](https://docs.logto.io/quick-starts/php/#get-user-information).

</Step>

</Steps>

0 comments on commit 01558bb

Please sign in to comment.