Skip to content

Commit d9fe9f5

Browse files
authored
fix(data): swift code snippets for customize auth (#7604)
1 parent 58b68f0 commit d9fe9f5

File tree

6 files changed

+199
-2
lines changed

6 files changed

+199
-2
lines changed

src/pages/[platform]/build-a-backend/data/customize-authz/index.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ const schema = a.schema({
164164

165165
On the client side, make sure to always authenticate with the corresponding authorization mode.
166166

167+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
168+
167169
```ts
168170
import { generateClient } from 'aws-amplify/data'
169171
import type { Schema } from '@/amplify/data/resource' // Path to your backend resource definition
@@ -184,6 +186,38 @@ const { data: listPostsResult , errors } = await client.models.Post.list({
184186
});
185187
```
186188

189+
</InlineFilter>
190+
191+
<InlineFilter filters={["swift"]}>
192+
193+
Creating a post is restricted to Cognito User Pools.
194+
195+
```swift
196+
do {
197+
let post = Post(title: "Hello World")
198+
let createdTodo = try await Amplify.API.mutate(request: .create(
199+
post,
200+
authMode: .amazonCognitoUserPools)).get()
201+
} catch {
202+
print("Failed to create post", error)
203+
}
204+
```
205+
206+
Listing posts is available to unauthenticated users (verified by Amazon Cognito identity pool's unauthenticated role)
207+
208+
```swift
209+
do {
210+
let queriedPosts = try await Amplify.API.query(request: .list(
211+
Post.self,
212+
authMode: .awsIAM)).get()
213+
print("Number of posts:", queriedPosts.count)
214+
} catch {
215+
print("Failed to list posts", error)
216+
}
217+
```
218+
219+
</InlineFilter>
220+
187221
## Learn more about specific authorization strategies
188222

189223
<Overview childPageNodes={props.childPageNodes} />

src/pages/[platform]/build-a-backend/data/customize-authz/multi-user-data-access/index.mdx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ const schema = a.schema({
4141
Todo: a
4242
.model({
4343
content: a.string(),
44+
owners: a.string().array(),
4445
})
4546
.authorization(allow => [allow.ownersDefinedIn('owners')]),
4647
});
4748
```
4849

50+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
51+
4952
In your application, you can perform CRUD operations against the model using `client.models.<model-name>` with the `userPool` auth mode.
5053

5154
```ts
@@ -66,8 +69,10 @@ const { errors, data: newTodo } = await client.models.Todo.create(
6669
// highlight-end
6770
);
6871
```
72+
73+
Add another user as an owner
74+
6975
```ts
70-
// Add another user as an owner
7176
await client.models.Todo.update(
7277
{
7378
id: newTodo.id,
@@ -81,6 +86,38 @@ await client.models.Todo.update(
8186
);
8287
```
8388

89+
</InlineFilter>
90+
91+
<InlineFilter filters={["swift"]}>
92+
93+
In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode.
94+
95+
```swift
96+
do {
97+
let todo = Todo(content: "My new todo")
98+
let createdTodo = try await Amplify.API.mutate(request: .create(
99+
todo,
100+
authMode: .amazonCognitoUserPools)).get()
101+
} catch {
102+
print("Failed to create todo", error)
103+
}
104+
```
105+
106+
Add another user as an owner
107+
108+
```swift
109+
do {
110+
createdTodo.owners?.append(otherUserId)
111+
let updatedTodo = try await Amplify.API.mutate(request: .update(
112+
createdTodo,
113+
authMode: .amazonCognitoUserPools)).get()
114+
} catch {
115+
print("Failed to update todo", error)
116+
}
117+
```
118+
119+
</InlineFilter>
120+
84121
## Override to a list of owners
85122

86123
You can override the `inField` to a list of owners. Use this if you want a dynamic set of users to have access to a record. In the example below, the `authors` list is populated with the creator of the record upon record creation. The creator can then update the `authors` field with additional users. Any user listed in the `authors` field can access the record.

src/pages/[platform]/build-a-backend/data/customize-authz/per-user-per-owner-data-access/index.mdx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ The `owner` authorization strategy restricts operations on a record to only the
3636

3737
You can use the `owner` authorization strategy to restrict a record's access to a specific user. When `owner` authorization is configured, only the record's `owner` is allowed the specified operations.
3838

39-
4039
```ts title="amplify/data/resource.ts"
4140
// The "owner" of a Todo is allowed to create, read, update, and delete their own todos
4241
const schema = a.schema({
@@ -60,6 +59,8 @@ const schema = a.schema({
6059
});
6160
```
6261

62+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
63+
6364
In your application, you can perform CRUD operations against the model using `client.models.<model-name>` with the `userPool` auth mode.
6465

6566
```ts
@@ -79,6 +80,23 @@ const { errors, data: newTodo } = await client.models.Todo.create(
7980
// highlight-end
8081
);
8182
```
83+
</InlineFilter>
84+
85+
<InlineFilter filters={["swift"]}>
86+
87+
In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode.
88+
89+
```swift
90+
do {
91+
let todo = Todo(content: "My new todo")
92+
let createdTodo = try await Amplify.API.mutate(request: .create(
93+
todo,
94+
authMode: .amazonCognitoUserPools)).get()
95+
} catch {
96+
print("Failed to create todo", error)
97+
}
98+
```
99+
</InlineFilter>
82100

83101
Behind the scenes, Amplify will automatically add a `owner: a.string()` field to each record which contains the record owner's identity information upon record creation.
84102

src/pages/[platform]/build-a-backend/data/customize-authz/public-data-access/index.mdx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const schema = a.schema({
4545
});
4646
```
4747

48+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
49+
4850
In your application, you can perform CRUD operations against the model using `client.models.<model-name>` by specifying the `apiKey` auth mode.
4951

5052
```ts
@@ -65,6 +67,25 @@ const { errors, data: newTodo } = await client.models.Todo.create(
6567
);
6668
```
6769

70+
</InlineFilter>
71+
72+
<InlineFilter filters={["swift"]}>
73+
74+
In your application, you can perform CRUD operations against the model by specifying the `apiKey` auth mode.
75+
76+
```swift
77+
do {
78+
let todo = Todo(content: "My new todo")
79+
let createdTodo = try await Amplify.API.mutate(request: .create(
80+
todo,
81+
authMode: .apiKey)).get()
82+
} catch {
83+
print("Failed to create todo", error)
84+
}
85+
```
86+
87+
</InlineFilter>
88+
6889
## Add public authorization rule using Amazon Cognito identity pool's unauthenticated role
6990

7091
You can also override the authorization provider. In the example below, `identityPool` is specified as the provider which allows you to use an "Unauthenticated Role" from the Cognito identity pool for public access instead of an API key. Your Auth resources defined in `amplify/auth/resource.ts` generates scoped down IAM policies for the "Unauthenticated role" in the Cognito identity pool automatically.
@@ -79,6 +100,8 @@ const schema = a.schema({
79100
});
80101
```
81102

103+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
104+
82105
In your application, you can perform CRUD operations against the model using `client.models.<model-name>` with the `identityPool` auth mode.
83106

84107
<Callout info>
@@ -123,3 +146,22 @@ const { errors, data: newTodo } = await client.models.Todo.create(
123146
// highlight-end
124147
);
125148
```
149+
150+
</InlineFilter>
151+
152+
<InlineFilter filters={["swift"]}>
153+
154+
In your application, you can perform CRUD operations against the model with the `awsIAM` auth mode.
155+
156+
```swift
157+
do {
158+
let todo = Todo(content: "My new todo")
159+
let createdTodo = try await Amplify.API.mutate(request: .create(
160+
todo,
161+
authMode: .awsIAM)).get()
162+
} catch {
163+
print("Failed to create todo", error)
164+
}
165+
```
166+
167+
</InlineFilter>

src/pages/[platform]/build-a-backend/data/customize-authz/signed-in-user-data-access/index.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const schema = a.schema({
5151
});
5252
```
5353

54+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
55+
5456
In your application, you can perform CRUD operations against the model using `client.models.<model-name>` with the `userPool` auth mode.
5557

5658
```ts
@@ -70,6 +72,24 @@ const { errors, data: newTodo } = await client.models.Todo.create(
7072
// highlight-end
7173
);
7274
```
75+
</InlineFilter>
76+
77+
<InlineFilter filters={["swift"]}>
78+
79+
In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode.
80+
81+
```swift
82+
do {
83+
let todo = Todo(content: "My new todo")
84+
let createdTodo = try await Amplify.API.mutate(request: .create(
85+
todo,
86+
authMode: .amazonCognitoUserPools)).get()
87+
} catch {
88+
print("Failed to create todo", error)
89+
}
90+
```
91+
92+
</InlineFilter>
7393

7494
## Use identity pool for signed-in user authentication
7595

@@ -86,6 +106,8 @@ const schema = a.schema({
86106
});
87107
```
88108

109+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
110+
89111
In your application, you can perform CRUD operations against the model using `client.models.<model-name>` with the `iam` auth mode.
90112

91113
<Callout info>
@@ -109,5 +131,26 @@ const { errors, data: newTodo } = await client.models.Todo.create(
109131
// highlight-end
110132
);
111133
```
134+
</InlineFilter>
135+
136+
<InlineFilter filters={["swift"]}>
137+
138+
In your application, you can perform CRUD operations against the model with the `awsIAM` auth mode.
139+
140+
<Callout info>
141+
The user must be logged in for the Amplify Library to use the authenticated role from your Cognito identity pool.
142+
</Callout>
143+
144+
```swift
145+
do {
146+
let todo = Todo(content: "My new todo")
147+
let createdTodo = try await Amplify.API.mutate(request: .create(
148+
todo,
149+
authMode: .awsIAM)).get()
150+
} catch {
151+
print("Failed to create todo", error)
152+
}
153+
```
154+
</InlineFilter>
112155

113156
In addition, you can also use OpenID Connect with `authenticated` authorization. See [OpenID Connect as an authorization provider](/[platform]/build-a-backend/data/customize-authz/using-oidc-authorization-provider/).

src/pages/[platform]/build-a-backend/data/customize-authz/user-group-based-data-access/index.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const schema = a.schema({
4747
});
4848
```
4949

50+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
51+
5052
In your application, you can perform CRUD operations against the model using `client.models.<model-name>` with the `userPool` auth mode.
5153

5254
```ts
@@ -69,6 +71,27 @@ const { errors, data: newSalary } = await client.models.Salary.create(
6971
);
7072
```
7173

74+
</InlineFilter>
75+
76+
<InlineFilter filters={["swift"]}>
77+
78+
In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode.
79+
80+
```swift
81+
do {
82+
let salary = Salary(
83+
wage: 50.25,
84+
currency: "USD")
85+
let createdSalary = try await Amplify.API.mutate(request: .create(
86+
salary,
87+
authMode: .amazonCognitoUserPools)).get()
88+
} catch {
89+
print("Failed to create salary", error)
90+
}
91+
```
92+
93+
</InlineFilter>
94+
7295
This can then be updated to allow access to multiple defined groups; in this example below we added access for "Leadership".
7396

7497
```ts

0 commit comments

Comments
 (0)