diff --git a/src/pages/[platform]/build-a-backend/auth/concepts/multi-factor-authentication/index.mdx b/src/pages/[platform]/build-a-backend/auth/concepts/multi-factor-authentication/index.mdx index 810bc7c43f6..fb326a52f93 100644 --- a/src/pages/[platform]/build-a-backend/auth/concepts/multi-factor-authentication/index.mdx +++ b/src/pages/[platform]/build-a-backend/auth/concepts/multi-factor-authentication/index.mdx @@ -117,7 +117,7 @@ Once you have setup SMS as your second layer of authentication with MFA as shown You will need to pass `phone_number` as a user attribute to enable SMS MFA for your users during sign-up. However, if the primary sign-in mechanism for your Cognito resource is `phone_number` (without enabling `username`), then you do not need to pass it as an attribute. - + ```ts import { signUp } from 'aws-amplify/auth'; @@ -133,6 +133,28 @@ await signUp({ }); ``` + + +```dart +Future signUpWithPhoneVerification( + String username, + String password, +) async { + await Amplify.Auth.signUp( + username: username, + password: password, + options: SignUpOptions( + userAttributes: { + // ... if required + AuthUserAttributeKey.email: 'test@example.com', + AuthUserAttributeKey.phoneNumber: '+18885551234', + }, + ), + ); +} +``` + + ```swift func signUp(username: String, password: String, email: String, phonenumber: String) async { @@ -161,7 +183,7 @@ func signUp(username: String, password: String, email: String, phonenumber: Stri By default, you have to verify a user account after they sign up using the `confirmSignUp` API, which will send a one-time password to the user's phone number or email, depending on your Amazon Cognito configuration. - + ```ts import { confirmSignUp } from 'aws-amplify/auth'; @@ -172,6 +194,20 @@ await confirmSignUp({ ``` + +```dart +Future confirmSignUpPhoneVerification( + String username, + String otpCode, +) async { + await Amplify.Auth.confirmSignUp( + username: username, + confirmationCode: otpCode, + ); +} +``` + + ```swift @@ -197,7 +233,7 @@ After a user signs in, if they have MFA enabled for their account, a challenge w If MFA is **ON** or enabled for the user, you must call `confirmSignIn` with the OTP sent to their phone. - + ```ts import { confirmSignIn } from 'aws-amplify/auth'; @@ -207,6 +243,15 @@ await confirmSignIn({ ``` + +```dart +Future confirmSignInPhoneVerification(String otpCode) async { + await Amplify.Auth.confirmSignIn( + confirmationValue: otpCode, + ); +} +``` + ```swift @@ -226,7 +271,7 @@ func confirmSignIn() async { After a user has been signed in, call `updateMFAPreference` to record the MFA type as enabled for the user and optionally set it as preferred so that subsequent logins default to using this MFA type. - + ```ts import { updateMFAPreference } from 'aws-amplify/auth'; @@ -234,6 +279,17 @@ await updateMFAPreference({ sms: 'PREFERRED' }); ``` + +```dart +Future updateMfaPreferences() async { + final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey); + + await cognitoPlugin.updateMfaPreference( + sms: MfaPreference.enabled, // or .preferred + ); +} +``` + ```swift @@ -287,7 +343,7 @@ You can use Time-based One-Time Password (TOTP) for multi-factor authentication ### Set up TOTP for a user - + After you initiate a user sign in with the `signIn` API where a user is required to set up TOTP as an MFA method, the API call will return `CONTINUE_SIGN_IN_WITH_TOTP_SETUP` as a challenge and next step to handle in your app. You will get that challenge if the following conditions are met: @@ -321,8 +377,7 @@ switch (nextStep.signInStep) { ``` - - + After you initiate a user sign in with the `signIn` API where a user is required to set up TOTP as an MFA method, the API call will return `continueSignInWithTOTPSetup` as a challenge and next step to handle in your app. You will get that challenge if the following conditions are met: - MFA is marked as **Required** in your user pool. @@ -332,6 +387,38 @@ After you initiate a user sign in with the `signIn` API where a user is required The `continueSignInWithTOTPSetup` step signifies that the user must set up TOTP before they can sign in. The step returns an associated value of type `TOTPSetupDetails` which must be used to configure an authenticator app like Microsoft Authenticator or Google Authenticator. `TOTPSetupDetails` provides a helper method called `getSetupURI` which generates a URI that can be used, for example, in a button to open the user's installed authenticator app. For more advanced use cases, `TOTPSetupDetails` also contains a `sharedSecret` which can be used to either generate a QR code or be manually entered into an authenticator app. Once the authenticator app is set up, the user can generate a TOTP code and provide it to the library to complete the sign in process. + + + + +```dart +Future signInUser(String username, String password) async { + try { + final result = await Amplify.Auth.signIn( + username: username, + password: password, + ); + return _handleSignInResult(result); + } on AuthException catch (e) { + safePrint('Error signing in: ${e.message}'); + } +} + +Future _handleSignInResult(SignInResult result) async { + switch (result.nextStep.signInStep) { + // ··· + case AuthSignInStep.continueSignInWithTotpSetup: + final totpSetupDetails = result.nextStep.totpSetupDetails!; + final setupUri = totpSetupDetails.getSetupUri(appName: 'MyApp'); + safePrint('Open URI to complete setup: $setupUri'); + // ··· + } +} +``` + + + + ```swift func signIn(username: String, password: String) async { do { @@ -365,7 +452,7 @@ func signIn(username: String, password: String) async { The TOTP code can be obtained from the user via a text field or any other means. Once the user provides the TOTP code, call `confirmSignIn` with the TOTP code as the `challengeResponse` parameter. - + ```ts import { confirmSignIn } from 'aws-amplify/auth'; @@ -375,6 +462,21 @@ await confirmSignIn({ ``` + +```dart +Future confirmTotpUser(String totpCode) async { + try { + final result = await Amplify.Auth.confirmSignIn( + confirmationValue: totpCode, + ); + return _handleSignInResult(result); + } on AuthException catch (e) { + safePrint('Error confirming TOTP code: ${e.message}'); + } +} +``` + + ```swift func confirmSignIn() async { @@ -393,7 +495,7 @@ func confirmSignIn() async { After a user has been signed in, call `updateMFAPreference` to record the MFA type as enabled for the user and optionally set it as preferred so that subsequent logins default to using this MFA type. - + ```ts import { updateMFAPreference } from 'aws-amplify/auth'; @@ -401,6 +503,18 @@ await updateMFAPreference({ totp: 'PREFERRED' }); ``` + +```dart +Future updateMfaPreferences() async { + final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey); + + await cognitoPlugin.updateMfaPreference( + totp: MfaPreference.preferred, + ); +} +``` + + ```swift func updateMFAPreferences() async throws { @@ -428,7 +542,7 @@ Invoke the `setUpTOTP` API to generate a `TOTPSetupDetails` object which should that contains the `sharedSecret` which will be used to either to generate a QR code or can be manually entered into an Authenticator app. - + ```ts import { setUpTOTP } from 'aws-amplify/auth'; @@ -439,6 +553,20 @@ const setupUri = totpSetupDetails.getSetupUri(appName); ``` + +```dart +Future setUpTotp() async { + try { + final totpSetupDetails = await Amplify.Auth.setUpTotp(); + final setupUri = totpSetupDetails.getSetupUri(appName: 'MyApp'); + safePrint('Open URI to complete setup: $setupUri'); + } on AuthException catch (e) { + safePrint('An error occurred setting up TOTP: $e'); + } +} +``` + + ```swift func setUpTOTP() async { @@ -464,7 +592,7 @@ func setUpTOTP() async { Once the Authenticator app is set up, the user must generate a TOTP code and provide it to the library. Pass the code to `verifyTOTPSetup` to complete the TOTP setup process. - + ```ts import { verifyTOTPSetup } from 'aws-amplify/auth'; @@ -472,6 +600,18 @@ await verifyTOTPSetup({ code: "123456" }); ``` + +```dart +Future verifyTotpSetup(String totpCode) async { + try { + await Amplify.Auth.verifyTotpSetup(totpCode); + } on AuthException catch (e) { + safePrint('An error occurred verifying TOTP: $e'); + } +} +``` + + ```swift func verifyTOTPSetup(totpCodeFromAuthenticatorApp: String) async { @@ -487,7 +627,7 @@ func verifyTOTPSetup(totpCodeFromAuthenticatorApp: String) async { After TOTP setup is complete, call `updateMFAPreference` to record the MFA type as enabled for the user and optionally set it as preferred so that subsequent logins default to using this MFA type. - + ```ts import { updateMFAPreference } from 'aws-amplify/auth'; @@ -495,6 +635,19 @@ await updateMFAPreference({ sms: 'ENABLED', totp: 'PREFERRED' }); ``` + +```dart +Future updateMfaPreferences() async { + final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey); + + await cognitoPlugin.updateMfaPreference( + sms: MfaPreference.enabled, + totp: MfaPreference.preferred, + ); +} +``` + + ```swift func updateMFAPreferences() async throws { @@ -527,7 +680,7 @@ In a scenario where MFA is marked as "Required" in the Cognito User Pool and ano Invoke the following API to get the current MFA preference and enabled MFA types, if any, for the current user. - + ```ts import { fetchMFAPreference } from 'aws-amplify/auth'; @@ -535,6 +688,18 @@ const { enabled, preferred } = await fetchMFAPreference(); ``` + +```dart +Future getCurrentMfaPreference() async { + final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey); + + final currentPreference = await cognitoPlugin.fetchMfaPreference(); + safePrint('Enabled MFA types for user: ${currentPreference.enabled}'); + safePrint('Preferred MFA type for user: ${currentPreference.preferred}'); +} +``` + + ```swift func getMFAPreferences() async throws { @@ -559,7 +724,7 @@ Only one MFA method can be marked as preferred at a time. If the user has multip - + ```ts import { updateMFAPreference } from 'aws-amplify/auth'; @@ -567,6 +732,19 @@ await updateMFAPreference({ sms: 'ENABLED', totp: 'PREFERRED' }); ``` + +```dart +Future updateMfaPreferences() async { + final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey); + + await cognitoPlugin.updateMfaPreference( + sms: MfaPreference.enabled, + totp: MfaPreference.preferred, + ); +} +``` + + ```swift func updateMFAPreferences() async throws { @@ -583,7 +761,7 @@ func updateMFAPreferences() async throws { ``` - + If multiple MFA methods are enabled for the user, the `signIn` API will return `CONTINUE_SIGN_IN_WITH_MFA_SELECTION` as the next step in the auth flow. During this scenario, the user should be prompted to select the MFA method they want to use to sign in and their preference should be passed to `confirmSignIn`. ```ts @@ -625,9 +803,42 @@ async function handleMFASelection(mfaType: 'SMS' | 'TOTP') { ``` - - + If multiple MFA methods are enabled for the user, the `signIn` API will return `continueSignInWithMFASelection` as the next step in the auth flow. During this scenario, the user should be prompted to select the MFA method they want to use to sign in and their preference should be passed to `confirmSignIn`. + + + + +```dart +Future _handleSignInResult(SignInResult result) async { + switch (result.nextStep.signInStep) { + // ··· + case AuthSignInStep.continueSignInWithMfaSelection: + final allowedMfaTypes = result.nextStep.allowedMfaTypes!; + final selection = await _promptUserPreference(allowedMfaTypes); + return _handleMfaSelection(selection); + // ··· + } +} + +Future _promptUserPreference(Set allowedTypes) async { + // ··· +} + +Future _handleMfaSelection(MfaType selection) async { + try { + final result = await Amplify.Auth.confirmSignIn( + confirmationValue: selection.confirmationValue, + ); + return _handleSignInResult(result); + } on AuthException catch (e) { + safePrint('Error resending code: ${e.message}'); + } +} +``` + + + ```swift func signIn(username: String, password: String) async { diff --git a/src/pages/[platform]/build-a-backend/auth/concepts/tokens-and-credentials/index.mdx b/src/pages/[platform]/build-a-backend/auth/concepts/tokens-and-credentials/index.mdx index ae211e998d3..f2b924c3f04 100644 --- a/src/pages/[platform]/build-a-backend/auth/concepts/tokens-and-credentials/index.mdx +++ b/src/pages/[platform]/build-a-backend/auth/concepts/tokens-and-credentials/index.mdx @@ -253,10 +253,14 @@ class InMemoryStorage implements SecureStorageInterface { ## Token Revocation - + Token revocation is enabled automatically in Amplify Auth. To revoke tokens you can set up global sign-out with `signOut({ global: true })` to globally sign out your user from all of their devices. + +Token revocation is enabled automatically in Amplify Auth. To revoke tokens you can invoke `await Amplify.Auth.signOut(options: const signOutOptions(globalSignOut: true))` to globally sign out your user from all of their devices. + + Token revocation is enabled automatically in Amplify Auth. To revoke tokens you can invoke `await Amplify.Auth.signOut(options: .init(globalSignOut: true))` to globally sign out your user from all of their devices. diff --git a/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-in/index.mdx b/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-in/index.mdx index 34abae81352..5087c231df3 100644 --- a/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-in/index.mdx +++ b/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-in/index.mdx @@ -253,7 +253,7 @@ func signIn(username: String, password: String) -> AnyCancellable { The `signIn` API response will include a `nextStep` property, which can be used to determine if further action is required. It may return the following next steps: - + - `CONFIRM_SIGN_IN_WITH_NEW_PASSWORD_REQUIRED` - The user was created with a temporary password and must set a new one. Complete the process with `confirmSignIn`. - `CONFIRM_SIGN_IN_WITH_CUSTOM_CHALLENGE` - The sign-in must be confirmed with a custom challenge response. Complete the process with `confirmSignIn`. - `CONFIRM_SIGN_IN_WITH_TOTP_CODE` - The sign-in must be confirmed with a TOTP code from the user. Complete the process with `confirmSignIn`. @@ -265,7 +265,7 @@ The `signIn` API response will include a `nextStep` property, which can be used - `DONE` - The sign in process has been completed. - + - `confirmSignInWithNewPassword` - The user was created with a temporary password and must set a new one. Complete the process with `confirmSignIn`. - `confirmSignInWithCustomChallenge` - The sign-in must be confirmed with a custom challenge response. Complete the process with `confirmSignIn`. - `confirmSignInWithTOTPCode` - The sign-in must be confirmed with a TOTP code from the user. Complete the process with `confirmSignIn`. diff --git a/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-up/index.mdx b/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-up/index.mdx index 45056d61d69..794d6b3ddf3 100644 --- a/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-up/index.mdx +++ b/src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-up/index.mdx @@ -285,13 +285,13 @@ func signUp(username: String, password: String, email: String, phonenumber: Stri The `signUp` API response will include a `nextStep` property, which can be used to determine if further action is required. It may return the following next steps: - + - `CONFIRM_SIGN_UP` - The sign up needs to be confirmed by collecting a code from the user and calling `confirmSignUp`. - `DONE` - The sign up process has been fully completed. - `COMPLETE_AUTO_SIGN_IN` - The sign up process needs to complete by invoking the `autoSignIn` API. - + - `confirmUser` - The sign up needs to be confirmed by collecting a code from the user and calling `confirmSignUp` API. - `done` - The sign up process has been fully completed. diff --git a/src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx b/src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx index 6b3ba176d93..211b0e429ab 100644 --- a/src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx +++ b/src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx @@ -410,7 +410,7 @@ Future main() async { Future _configureAmplify() async { try { await Amplify.addPlugin(AmplifyAuthCognito()); - await Amplify.configure(outputs); + await Amplify.configure(amplifyConfig); safePrint('Successfully configured'); } on Exception catch (e) { safePrint('Error configuring Amplify: $e'); diff --git a/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/index.mdx b/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/index.mdx index 5d855652f22..4d41899c858 100644 --- a/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/index.mdx @@ -9,7 +9,6 @@ export const meta = { platforms: [ 'android', 'angular', - 'flutter', 'javascript', 'nextjs', 'react', diff --git a/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/nextjs-server-runtime/index.mdx b/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/nextjs-server-runtime/index.mdx index 8eefe009027..865e3835d7e 100644 --- a/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/nextjs-server-runtime/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/connect-from-server-runtime/nextjs-server-runtime/index.mdx @@ -7,7 +7,6 @@ export const meta = { platforms: [ 'android', 'angular', - 'flutter', 'javascript', 'nextjs', 'react', diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/configure-custom-identity-and-group-claim/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/configure-custom-identity-and-group-claim/index.mdx index 6327f17611f..5c311e14155 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/configure-custom-identity-and-group-claim/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/configure-custom-identity-and-group-claim/index.mdx @@ -56,6 +56,8 @@ export const data = defineData({ schema }); ``` + + In your application, you can perform CRUD operations against the model using `client.models.` with the `userPool` auth mode. ```ts @@ -76,3 +78,29 @@ const { errors, data: newTodo } = await client.models.Todo.create( // highlight-end ); ``` + + + + +In your application, you can perform CRUD operations against the model with the `userPools` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.userPools, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/custom-data-access-patterns/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/custom-data-access-patterns/index.mdx index 85f77707043..dd17b9f71e8 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/custom-data-access-patterns/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/custom-data-access-patterns/index.mdx @@ -68,6 +68,7 @@ export const data = defineData({ }, }); ``` + In your application, you can perform CRUD operations against the model using `client.models.` with the `lambda` auth mode. @@ -89,6 +90,33 @@ const { errors, data: newTodo } = await client.models.Todo.create( ); ``` + + + +In your application, you can perform CRUD operations against the model with the `function` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.function, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + + The Lambda function of choice will receive an authorization token from the client and execute the desired authorization logic. The AppSync GraphQL API will receive a payload from Lambda after invocation to allow or deny the API call accordingly. To configure a Lambda function as the authorization mode, create a new file `amplify/data/custom-authorizer.ts`. You can use this Lambda function code template as a starting point for your authorization handler code: diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/multi-user-data-access/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/multi-user-data-access/index.mdx index 9d3d1b8b416..5a29631b348 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/multi-user-data-access/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/multi-user-data-access/index.mdx @@ -47,7 +47,7 @@ const schema = a.schema({ }); ``` - + In your application, you can perform CRUD operations against the model using `client.models.` with the `userPool` auth mode. @@ -88,6 +88,51 @@ await client.models.Todo.update( + +In your application, you can perform CRUD operations against the model with the `userPools` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.userPools, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + +Add another user as an owner + +```dart +try { + createdTodo.owners!.add(otherUserId); + let updateRequest = ModelMutations.update( + createdTodo, + authorizationMode: APIAuthorizationType.userPools, + ); + final updatedTodo = await Amplify.API.mutations(request: updateRequest).response; + + if (updatedTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + +} catch { + safePrint("Failed to update todo", error) +} +``` + + In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode. diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/per-user-per-owner-data-access/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/per-user-per-owner-data-access/index.mdx index f2305085277..1abb86bb0c7 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/per-user-per-owner-data-access/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/per-user-per-owner-data-access/index.mdx @@ -59,7 +59,7 @@ const schema = a.schema({ }); ``` - + In your application, you can perform CRUD operations against the model using `client.models.` with the `userPool` auth mode. @@ -82,6 +82,30 @@ const { errors, data: newTodo } = await client.models.Todo.create( ``` + +In your application, you can perform CRUD operations against the model with the `userPools` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.userPools, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode. diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/public-data-access/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/public-data-access/index.mdx index cd07fbed6e9..df2beb9f9f6 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/public-data-access/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/public-data-access/index.mdx @@ -45,7 +45,7 @@ const schema = a.schema({ }); ``` - + In your application, you can perform CRUD operations against the model using `client.models.` by specifying the `apiKey` auth mode. @@ -69,9 +69,35 @@ const { errors, data: newTodo } = await client.models.Todo.create( - - + In your application, you can perform CRUD operations against the model by specifying the `apiKey` auth mode. + + + + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.apiKey, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + + + ```swift do { @@ -149,8 +175,32 @@ const { errors, data: newTodo } = await client.models.Todo.create( - + +In your application, you can perform CRUD operations against the model with the `iam` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.iam, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + + + In your application, you can perform CRUD operations against the model with the `awsIAM` auth mode. ```swift diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/signed-in-user-data-access/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/signed-in-user-data-access/index.mdx index 55715c8152a..3ad29a63f60 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/signed-in-user-data-access/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/signed-in-user-data-access/index.mdx @@ -51,7 +51,7 @@ const schema = a.schema({ }); ``` - + In your application, you can perform CRUD operations against the model using `client.models.` with the `userPool` auth mode. @@ -74,6 +74,30 @@ const { errors, data: newTodo } = await client.models.Todo.create( ``` + +In your application, you can perform CRUD operations against the model with the `userPools` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.userPools, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode. @@ -106,7 +130,7 @@ const schema = a.schema({ }); ``` - + In your application, you can perform CRUD operations against the model using `client.models.` with the `iam` auth mode. @@ -133,6 +157,31 @@ const { errors, data: newTodo } = await client.models.Todo.create( ``` + +In your application, you can perform CRUD operations against the model with the `iam` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.iam, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + + In your application, you can perform CRUD operations against the model with the `awsIAM` auth mode. diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/user-group-based-data-access/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/user-group-based-data-access/index.mdx index be25af69d33..934b3421718 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/user-group-based-data-access/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/user-group-based-data-access/index.mdx @@ -47,7 +47,7 @@ const schema = a.schema({ }); ``` - + In your application, you can perform CRUD operations against the model using `client.models.` with the `userPool` auth mode. @@ -73,6 +73,30 @@ const { errors, data: newSalary } = await client.models.Salary.create( + +In your application, you can perform CRUD operations against the model with the `userPools` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.userPools, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + In your application, you can perform CRUD operations against the model with the `amazonCognitoUserPools` auth mode. diff --git a/src/pages/[platform]/build-a-backend/data/customize-authz/using-oidc-authorization-provider/index.mdx b/src/pages/[platform]/build-a-backend/data/customize-authz/using-oidc-authorization-provider/index.mdx index 4fcf5fad7ef..18493cb1302 100644 --- a/src/pages/[platform]/build-a-backend/data/customize-authz/using-oidc-authorization-provider/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/customize-authz/using-oidc-authorization-provider/index.mdx @@ -68,6 +68,8 @@ export const data = defineData({ }); ``` + + In your application, you can perform CRUD operations against the model using `client.models.` by specifying the `oidc` auth mode. ```ts @@ -82,3 +84,30 @@ const { errors, data: todos } = await client.models.Todo.list({ // highlight-end }); ``` + + + + +In your application, you can perform CRUD operations against the model with the `oidc` auth mode. + +```dart +try { + final todo = Todo(content: 'My new todo'); + final request = ModelMutations.create( + todo, + authorizationMode: APIAuthorizationType.oidc, + ); + final createdTodo = await Amplify.API.mutations(request: request).response; + + if (createdTodo == null) { + safePrint('errors: ${response.errors}'); + return; + } + safePrint('Mutation result: ${createdTodo.name}'); + +} on APIException catch (e) { + safePrint('Failed to create todo', e); +} +``` + + diff --git a/src/pages/[platform]/build-a-backend/data/set-up-data/index.mdx b/src/pages/[platform]/build-a-backend/data/set-up-data/index.mdx index 55c396b77fb..f6f9f6b08bd 100644 --- a/src/pages/[platform]/build-a-backend/data/set-up-data/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/set-up-data/index.mdx @@ -339,7 +339,11 @@ import 'models/ModelProvider.dart'; Future main() async { // highlight-start try { - final api = AmplifyAPI(modelProvider: ModelProvider.instance); + final api = AmplifyAPI( + options: APIPluginOptions( + modelProvider: ModelProvider.instance + ) + ); await Amplify.addPlugins([api]); await Amplify.configure(outputs); diff --git a/src/pages/[platform]/build-a-backend/data/subscribe-data/index.mdx b/src/pages/[platform]/build-a-backend/data/subscribe-data/index.mdx index 878624edb7d..b723dda2678 100644 --- a/src/pages/[platform]/build-a-backend/data/subscribe-data/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/subscribe-data/index.mdx @@ -554,11 +554,13 @@ Likewise, when disconnected from the internet unexpectedly, the subscription wil ```dart Future _configureAmplify() async { final apiPlugin = AmplifyAPI( - modelProvider: ModelProvider.instance, - // Optional config - subscriptionOptions: const GraphQLSubscriptionOptions( - retryOptions: RetryOptions(maxAttempts: 10), - ), + options: APIPluginOptions( + modelProvider: ModelProvider.instance, + // Optional config + subscriptionOptions: const GraphQLSubscriptionOptions( + retryOptions: RetryOptions(maxAttempts: 10), + ), + ) ); await Amplify.addPlugin(apiPlugin);