Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(console): fix php guide #6143

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading