Skip to content

Commit

Permalink
refactor: update swift sdk guide
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Feb 27, 2024
1 parent 87c132e commit 8b2505a
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 35 deletions.
7 changes: 0 additions & 7 deletions docs/sdk/fragments/_apply-authorization-token.md

This file was deleted.

7 changes: 0 additions & 7 deletions docs/sdk/fragments/_get-authorization-token.md

This file was deleted.

29 changes: 8 additions & 21 deletions docs/sdk/native/swift/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ sidebar_label: iOS (Swift)
---

import AppNote from '../../fragments/_app-note.mdx';
import ApplyAuthorizationToken from '../../fragments/_apply-authorization-token.md';
import FetchUserInformation from '../../fragments/_fetch-user-information.mdx';
import FurtherReadings from '../../fragments/_further-readings.md';
import GetAuthorizationToken from '../../fragments/_get-authorization-token.md';

import AddSdk from './_add-sdk.mdx';
import ImplementSignIn from './_implement-sign-in.mdx';
import InitClient from './_init-client.mdx';
import ApiResources from './api-resources/_index.mdx';

# iOS: Integrate Logto Swift SDK

Expand Down Expand Up @@ -41,29 +40,17 @@ await client.signOut()

<FetchUserInformation apiCodeFragment="logtoClient.fetchUserInfo()" />

## Backend API authorization
## API resources

<GetAuthorizationToken />
<ApiResources />

Add your API resource indicators to the Logto SDK configs:
### Attach acess token to request headers

```swift
let config = try? LogtoConfig(
endpoint: "<your-logto-endpoint>", // E.g. http://localhost:3001
appId: "<your-app-id>",
resources: ["<your-resource-indicators>"]
)
let client = LogtoClient(useConfig: config)
```

Claim an authorization token from Logto before making your API request:

```swift
let accessToken = try await client.getAccessToken(for: "<your-target-api-resource>")
// custom logic
```
Put the token in the `Authorization` field of HTTP headers with the Bearer format (`Bearer YOUR_TOKEN`), and you are good to go.

<ApplyAuthorizationToken />
:::note
The Bearer Token's integration flow may vary based on the framework or requester you are using. Choose your own way to apply the request `Authorization` header.
:::

```swift
await LogtoRequest.get(
Expand Down
11 changes: 11 additions & 0 deletions docs/sdk/native/swift/api-resources/_config-api-resources.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ConfigApiResources from '../../../fragments/_config-api-resources.mdx';

import ConfigResourcesCode from './code/_config-resources-code.md';
import ConfigResourcesWithScopesCode from './code/_config-resources-with-scopes-code.md';
import ConfigResourcesWithSharedScopesCode from './code/_config-resources-with-shared-scopes-code.md';

<ConfigApiResources
configResourcesCode={<ConfigResourcesCode />}
configResourcesWithScopesCode={<ConfigResourcesWithScopesCode />}
configResourcesWithSharedScopesCode={<ConfigResourcesWithSharedScopesCode />}
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FetchAccessTokenForApiResources from '../../../fragments/_fetch-access-token-for-api-resources.mdx';

import GetAccessTokenCode from './code/_get-access-token-code.md';

<FetchAccessTokenForApiResources
getAccessTokenApi="getAccessToken"
getAccessTokenCode={<GetAccessTokenCode />}
/>
16 changes: 16 additions & 0 deletions docs/sdk/native/swift/api-resources/_index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApiResourcesDescription from '../../../fragments/_api-resources-description.md';

import ConfigApiResources from './_config-api-resources.mdx';
import FetchAccessTokenForApiResources from './_fetch-access-token-for-api-resources.mdx';

<ApiResourcesDescription />

### Configure Logto client

<ConfigApiResources />

### Fetch access token for the API resource

<FetchAccessTokenForApiResources />

{/* Swift does not support organization tokens yet */}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
```swift
let config = try? LogtoConfig(
endpoint: "<your-logto-endpoint>", // E.g. http://localhost:3001
appId: "<your-app-id>",
resources: ["https://shopping.your-app.com/api", "https://store.your-app.com/api"], // Add API resources
)
let client = LogtoClient(useConfig: config)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```swift
let config = try? LogtoConfig(
endpoint: "<your-logto-endpoint>",
appId: "<your-app-id>",
scopes: ["shopping:read", "shopping:write", "store:read", "store:write"],
resources: ["https://shopping.your-app.com/api", "https://store.your-app.com/api"],
)
let client = LogtoClient(useConfig: config)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```swift
let config = try? LogtoConfig(
endpoint: "<your-logto-endpoint>",
appId: "<your-app-id>",
scopes: ["read", "write"],
resources: ["https://shopping.your-app.com/api", "https://store.your-app.com/api"],
)
let client = LogtoClient(useConfig: config)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```swift
let accessToken = try await client.getAccessToken(for: "https://shopping.your-app.com/api")
// Custom logic
```

0 comments on commit 8b2505a

Please sign in to comment.