-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reorg python and php docs (#730)
* refactor: reorg python docs * docs: improve php content * docs: improve python content --------- Co-authored-by: Gao Sun <[email protected]>
- Loading branch information
Showing
34 changed files
with
360 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
|
||
import CliamsNeedNetworkRequest from '../../fragments/_claims-need-network-request.mdx'; | ||
import FindUserInfoMissing from '../../fragments/_find-user-info-missing.mdx'; | ||
import ScopesAndClaims from '../../fragments/_scopes-and-claims.mdx'; | ||
import ScopesAndClaimsIntroduction from '../../fragments/_scopes-claims-introduction.md'; | ||
|
||
### Display user information | ||
|
||
To display the user's information, you can use either the `getIdTokenClaims` method or `fetchUserInfo` method to get user information. While `getIdTokenClaims` returns the user information contains in the ID token, `fetchUserInfo` fetches the user information from the userinfo endpoint. | ||
|
||
```php title="index.php" | ||
Route::get('/userinfo', 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()) | ||
); | ||
}); | ||
``` | ||
|
||
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. | ||
|
||
### Request additional claims | ||
|
||
<FindUserInfoMissing method="getIdTokenClaims" /> | ||
|
||
<ScopesAndClaimsIntroduction /> | ||
|
||
To request additional scopes, you can configure the `scopes` option when initializing the Logto client: | ||
|
||
```php title="index.php" | ||
$client = new LogtoClient( | ||
new LogtoConfig( | ||
// ...other configs | ||
// highlight-next-line | ||
scopes: ["email", "phone"], // Update per your needs | ||
), | ||
); | ||
``` | ||
|
||
Alternatively, you can use the `UserScope` enum to add scopes: | ||
|
||
```php title="index.php" | ||
// highlight-next-line | ||
use Logto\Sdk\Constants\UserScope; | ||
|
||
$client = new LogtoClient( | ||
new LogtoConfig( | ||
// ...other configs | ||
// highlight-next-line | ||
scopes: [UserScope::email, UserScope::phone], // Update per your needs | ||
), | ||
); | ||
``` | ||
|
||
Then the additional claims will be available in the return value of `getIdTokenClaims` or `fetchUserInfo`. | ||
|
||
<CliamsNeedNetworkRequest | ||
type="method" | ||
method="fetchUserInfo" | ||
codeSnippet={ | ||
<CodeBlock | ||
language="php" | ||
title="index.php" | ||
>{`$client->fetchUserInfo()->custom_data;`}</CodeBlock> | ||
} | ||
/> | ||
|
||
### Scopes and claims | ||
|
||
<ScopesAndClaims /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 18 additions & 30 deletions
48
docs/quick-starts/framework/php/_implement-sign-in-route.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,29 @@ | ||
import redirectUriFigure from '../../assets/go-redirect-uri.png'; | ||
import ConfigureRedirectUri from '../../fragments/_configure-redirect-uri.mdx'; | ||
import SignInFlowSummary from '../../fragments/_web-sign-in-flow-summary.mdx'; | ||
import CodeBlock from '@theme/CodeBlock'; | ||
|
||
import ImplementCallbackRoute from './_implement-callback-route.md'; | ||
import { | ||
defaultRedirectUri, | ||
defaultBaseUrl, | ||
} from '../../fragments/_web-configure-redirect-uris.mdx'; | ||
|
||
<SignInFlowSummary /> | ||
In your web application, add a route to properly handle the sign-in request from users. For example: | ||
|
||
Before you start implementing the sign-in flow, you need to add a redirect URI in the Admin Console for your application. | ||
<CodeBlock language="php"> | ||
{`Route::get('/sign-in', function () { | ||
return redirect($client->signIn('${defaultRedirectUri}')); | ||
});`} | ||
</CodeBlock> | ||
|
||
<ConfigureRedirectUri | ||
figureSrc={redirectUriFigure} | ||
redirectUri="http://localhost:8080/sign-in-callback" | ||
/> | ||
|
||
In your web application, add a route to properly handle the sign-in request from users. Let's use `/sign-in` as an example: | ||
|
||
```php | ||
Route::get('/sign-in', function () { | ||
return redirect($client->signIn('http://localhost:8080/sign-in-callback')); | ||
}); | ||
``` | ||
|
||
Replace `http://localhost:8080/sign-in-callback` with the callback URL you set in your Logto Console for this application. | ||
Replace <code>{defaultRedirectUri}</code> with the callback URL you set in your Logto Console for this application. | ||
|
||
If you want to show the sign-up page on the first screen, you can set `interactionMode` to `signUp`: | ||
|
||
```php | ||
Route::get('/sign-in', function () { | ||
return redirect($client->signIn('http://localhost:8080/sign-in-callback', InteractionMode::signUp)); | ||
}); | ||
``` | ||
<CodeBlock language="php"> | ||
{`Route::get('/sign-in', function () { | ||
return redirect($client->signIn('${defaultRedirectUri}', InteractionMode::signUp)); | ||
});`} | ||
</CodeBlock> | ||
|
||
Now, whenever your users visit `http://localhost:8080/sign-in`, it will start a new sign-in attempt and redirect the user to the Logto sign-in page. | ||
Now, whenever your users visit <code>{defaultBaseUrl}sign-in</code>, it will start a new sign-in attempt and redirect the user to the Logto sign-in page. | ||
|
||
> **Note** | ||
> Creating a sign-in route isn't the only way to start a sign-in attempt. You can always use the `signIn` method to get the sign-in URL and redirect the user to it. | ||
#### Implement the callback route | ||
|
||
<ImplementCallbackRoute /> |
20 changes: 8 additions & 12 deletions
20
docs/quick-starts/framework/php/_implement-sign-out-route.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,19 @@ | ||
import postSignOutRedirectUriFigure from '../../assets/go-post-sign-out-redirect-uri.png'; | ||
import ConfigurePostSignOutRedirectUri from '../../fragments/_configure-post-sign-out-redirect-uri.mdx'; | ||
import CodeBlock from '@theme/CodeBlock'; | ||
|
||
import ImplementSignOutRouteNotice from '../../fragments/_implement-sign-out-route-notice.md'; | ||
import { defaultBaseUrl } from '../../fragments/_web-configure-redirect-uris.mdx'; | ||
|
||
After the user makes a signing-out request, Logto will clear all user authentication information in the session. | ||
|
||
To clean up the PHP session and Logto session, a sign-out route can be implemented as follows: | ||
|
||
<ConfigurePostSignOutRedirectUri | ||
figureSrc={postSignOutRedirectUriFigure} | ||
postSignOutRedirectUri="http://localhost:8080" | ||
/> | ||
|
||
```php | ||
Route::get('/sign-out', function () { | ||
<CodeBlock language="php"> | ||
{`Route::get('/sign-out', function () { | ||
return redirect( | ||
// Redirect the user to the home page after a successful sign-out | ||
$client->signOut('http://localhost:8080') | ||
$client->signOut('${defaultBaseUrl}') | ||
); | ||
}); | ||
``` | ||
});`} | ||
</CodeBlock> | ||
|
||
<ImplementSignOutRouteNotice /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
docs/quick-starts/framework/php/api-resources/code/_config-organization-code.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
docs/quick-starts/framework/php/api-resources/code/_config-resources-code.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...k-starts/framework/php/api-resources/code/_config-resources-with-scopes-code.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
```php | ||
```php title="index.php" | ||
$client = new LogtoClient( | ||
new LogtoConfig( | ||
// ...other configs | ||
// highlight-start | ||
scopes: ["shopping:read", "shopping:write", "store:read", "store:write"], // Add scopes | ||
resources: ["https://shopping.your-app.com/api", "https://store.your-app.com/api"], // Add API resources | ||
// highlight-end | ||
), | ||
); | ||
``` |
4 changes: 3 additions & 1 deletion
4
...s/framework/php/api-resources/code/_config-resources-with-shared-scopes-code.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
```php | ||
```php title="index.php" | ||
$client = new LogtoClient( | ||
new LogtoConfig( | ||
// ...other configs | ||
// highlight-start | ||
scopes: ["read", "write"], // Add scopes | ||
resources: ["https://shopping.your-app.com/api", "https://store.your-app.com/api"], // Add API resources | ||
// highlight-end | ||
), | ||
); | ||
``` |
2 changes: 1 addition & 1 deletion
2
docs/quick-starts/framework/php/api-resources/code/_get-access-token-code.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
```php | ||
```php title="index.php" | ||
$accessToken = $client->getAccessToken("https://shopping.your-app.com/api"); | ||
``` |
6 changes: 3 additions & 3 deletions
6
...-starts/framework/php/api-resources/code/_get-organization-access-token-code.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
```php | ||
```php title="index.php" | ||
# Replace the parameter with a valid organization ID. | ||
# Valid organization IDs for the user can be found in the ID token claim `organizations`. | ||
$organizationToken = $client->getOrganizationToken("organization-id"); | ||
$organizationToken = $client->getOrganizationToken(organization_id); | ||
# or | ||
$claims = $client->getOrganizationTokenClaims("organization-id"); | ||
$claims = $client->getOrganizationTokenClaims(organization_id); | ||
``` |
Oops, something went wrong.