|
| 1 | +# AMPLIFYRULES |
| 2 | + |
| 3 | +- RULES THAT SHOULD BE ADHERED TO THE LAST WORD. |
| 4 | + |
| 5 | + 1. EXTERNAL PROVIDERS THAT ARE AVAILABLE ARE LISTED BELOW IN THE EXAMPLE, DON'T CHANGE THE NAMING CONVENTION WHILE USING THOSE IN THE CODE GENERATION. |
| 6 | + 2. DON'T FORGET TO IMPORT SECRET FOR ANY AUTHENTICATION BASED QUESTION. |
| 7 | + |
| 8 | + ```typescript |
| 9 | + import { defineAuth, secret } from "@aws-amplify/backend"; |
| 10 | + ``` |
| 11 | + |
| 12 | + 3. CALLBACK AND LOGOUT URLS SHOULD BE INSIDE THE "EXTERNALPROVIDERS" OBJECT. |
| 13 | + 4. WHILE ADDING THE CUSTOM ATTRIBUTES, IF THE ATTRIBUTE YOU ARE ADDING DOESNT BELONG TO THE STANDARD USER ATTRIBUTES LIST THEN ADD IT AS A CUSTOM ATTRIBUTE LIKE THIS "CUSTOM:ATTRIBUTE_NAME" AND THIS DOESN'T SUPPORT "REQUIRED" FIELD SO IGNORE IT WHILE GENERATING THE ANSWER. |
| 14 | + 5. WHILE ADDING THE CUSTOM ATTRIBUTES, MAKE SURE TO ALWAYS ADD THE "DATATYPE" FIELD AS IT IS A REQUIRED FIELD. |
| 15 | + 6. STATNDARD ATTIBUTES THAT ARE ALLOWED: `familyName`, `giveName`, `middleName`, `nickname`, `preferredUsername`, `profile`, `profilePicture`, `website`, `gender`, `birthdate`, `zoneinfo`, `locale`, `updatedAt`, `address`, `email`, `phoneNumber`, `sub`. THE `userAttributes` ARE SUPPOSED TO BE OUTSIDE THE `loginWith` OBJECT |
| 16 | + |
| 17 | + 7. THE FOLLOWING IS THE REQUIRED SYNTAX FOR `externalProviders`. ONLY THE FOUR LISTED PROVIDERS BELOW ARE SUPPORTED: |
| 18 | + |
| 19 | + ```typescript |
| 20 | + loginWith:{ |
| 21 | + //loginMethods |
| 22 | + externalProviders: { |
| 23 | + google: { |
| 24 | + |
| 25 | + }, |
| 26 | + signInWithApple: { |
| 27 | + }, |
| 28 | + loginWithAmazon: { |
| 29 | + |
| 30 | + }, |
| 31 | + facebook: { |
| 32 | + }, |
| 33 | + callbackUrls: [ |
| 34 | + // Callback URLs should be included inside the `externalProviders` object only, as per rule. |
| 35 | + |
| 36 | + ], |
| 37 | + logoutUrls: [ |
| 38 | + // Logout URLs should also be included inside `externalProviders` as per rule. |
| 39 | + |
| 40 | + ], |
| 41 | + }, |
| 42 | + } |
| 43 | + ``` |
| 44 | + |
| 45 | + 8. THE `userAttributes` ARE SUPPOSED TO BE OUTSIDE THE `loginWith` OBJECT |
| 46 | + `// Example configuration for user attributes and login methods |
| 47 | +loginWith: { |
| 48 | + // Specify login methods separately from user attributes, ONLY MENTION THE LOGINMETHODS THAT ARE SUPPOSED TO BE TRUE |
| 49 | + email: true, // Enable login with email |
| 50 | + phone: false, // Disable login with phone number |
| 51 | +},userAttributes: { |
| 52 | + email: true, // Attribute set as required |
| 53 | + phoneNumber: false, // Attribute not required |
| 54 | + givenName: true, // Attribute set as required |
| 55 | +},` |
| 56 | + 9. THIS IS THE WAY TO SETUP THE MULTIFACTOR AUTHENTHICATION |
| 57 | + export const auth = defineAuth({ |
| 58 | + loginWith: { |
| 59 | + email: true, |
| 60 | + phone: true, |
| 61 | + }, |
| 62 | + multifactor: { |
| 63 | + mode: "OPTIONAL", |
| 64 | + sms: true, |
| 65 | + totp: false, |
| 66 | + }, |
| 67 | + // IMPORTANT! THE LOGIC TO RESOLVE THIS VALUE CANNOT DETERMINE WHETHER EMAIL MFA IS ENABLED WHEN OVERRIDING THE RESOURCE. |
| 68 | + // BE SURE TO PICK A RECOVERY OPTION APPROPRIATE FOR YOUR APPLICATION. |
| 69 | + accountRecovery: "EMAIL_AND_PHONE_WITHOUT_MFA", |
| 70 | + senders: { |
| 71 | + email: { |
| 72 | + |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) |
| 76 | + 10. DON'T ADD THE `passwordPolicy` IN THE AUTH/RESOURCE.TS FILE. |
| 77 | + 11. THE `username` SHOULD BE REPLACED WITH `userAttributes` WHEN REQUESTED TO SETUP USERNAME AS A LOGIN METHOD AS USERNAME IS NOT A VALID LOGIN METHOD ON DEFINEAUTH, FOR EXAMPLE: |
| 78 | + |
| 79 | + ```typescript |
| 80 | + userAttributes: { |
| 81 | + preferredUsername: { |
| 82 | + mutable: true, |
| 83 | + required: false |
| 84 | + } |
| 85 | + } |
| 86 | + ``` |
| 87 | + |
| 88 | + 12. `loginWith` SUPPORTS ONLY TWO METHODS THAT IS `email` and `phone`. THERE IS NO `username` attribute to that. |
| 89 | + 13. THE `callbackUrls` AND `logoutUrls` SHOULD ONLY BE MENTIONED ONCE AS MENTIONED IN RULE #7 AND NOT FOR EACH EXTERNAL PROVIDER |
| 90 | + |
| 91 | +```typescript |
| 92 | +import { defineAuth, secret } from "@aws-amplify/backend"; |
| 93 | +
|
| 94 | +export const auth = defineAuth({ |
| 95 | + // Login Methods Configuration |
| 96 | + loginWith: { |
| 97 | + // Only email and phone are supported as login methods |
| 98 | + email: true, |
| 99 | + phone: true, |
| 100 | +
|
| 101 | + // External Providers Configuration - all providers shown with required fields |
| 102 | + externalProviders: { |
| 103 | + // Google Authentication |
| 104 | + google: { |
| 105 | + clientId: secret("GOOGLE_CLIENT_ID"), |
| 106 | + clientSecret: secret("GOOGLE_CLIENT_SECRET"), |
| 107 | + }, |
| 108 | + // Sign in with Apple |
| 109 | + signInWithApple: { |
| 110 | + clientId: secret("SIWA_CLIENT_ID"), |
| 111 | + keyId: secret("SIWA_KEY_ID"), |
| 112 | + privateKey: secret("SIWA_PRIVATE_KEY"), |
| 113 | + teamId: secret("SIWA_TEAM_ID"), |
| 114 | + }, |
| 115 | + // Login with Amazon |
| 116 | + loginWithAmazon: { |
| 117 | + clientId: secret("LOGINWITHAMAZON_CLIENT_ID"), |
| 118 | + clientSecret: secret("LOGINWITHAMAZON_CLIENT_SECRET"), |
| 119 | + }, |
| 120 | + // Facebook Authentication |
| 121 | + facebook: { |
| 122 | + clientId: secret("FACEBOOK_CLIENT_ID"), |
| 123 | + clientSecret: secret("FACEBOOK_CLIENT_SECRET"), |
| 124 | + }, |
| 125 | + // Callback and logout URLs must be inside externalProviders |
| 126 | + callbackUrls: [ |
| 127 | + "http://localhost:3000/profile", |
| 128 | + "https://mywebsite.com/profile", |
| 129 | + ], |
| 130 | + logoutUrls: ["http://localhost:3000/", "https://mywebsite.com"], |
| 131 | + }, |
| 132 | + }, |
| 133 | +
|
| 134 | + // User Attributes Configuration - outside loginWith |
| 135 | + userAttributes: { |
| 136 | + // Standard attributes examples |
| 137 | + email: { |
| 138 | + mutable: true, |
| 139 | + required: true, |
| 140 | + }, |
| 141 | + phoneNumber: { |
| 142 | + mutable: true, |
| 143 | + required: false, |
| 144 | + }, |
| 145 | + givenName: { |
| 146 | + mutable: true, |
| 147 | + required: true, |
| 148 | + }, |
| 149 | + familyName: { |
| 150 | + mutable: true, |
| 151 | + required: false, |
| 152 | + }, |
| 153 | + birthdate: { |
| 154 | + mutable: true, |
| 155 | + required: false, |
| 156 | + }, |
| 157 | + // Username configuration using preferredUsername |
| 158 | + preferredUsername: { |
| 159 | + mutable: true, |
| 160 | + required: false, |
| 161 | + }, |
| 162 | + // Additional standard attributes |
| 163 | + address: { |
| 164 | + mutable: true, |
| 165 | + required: false, |
| 166 | + }, |
| 167 | + gender: { |
| 168 | + mutable: true, |
| 169 | + required: false, |
| 170 | + }, |
| 171 | + locale: { |
| 172 | + mutable: true, |
| 173 | + required: false, |
| 174 | + }, |
| 175 | + profilePicture: { |
| 176 | + mutable: true, |
| 177 | + required: false, |
| 178 | + }, |
| 179 | + website: { |
| 180 | + mutable: true, |
| 181 | + required: false, |
| 182 | + }, |
| 183 | + // Custom attributes examples - note the 'custom:' prefix and required dataType |
| 184 | + "custom:organization": { |
| 185 | + dataType: "String", |
| 186 | + mutable: true, |
| 187 | + minLen: 3, |
| 188 | + maxLen: 100, |
| 189 | + }, |
| 190 | + "custom:employeeId": { |
| 191 | + dataType: "Number", |
| 192 | + mutable: false, |
| 193 | + min: 1000, |
| 194 | + max: 9999999, |
| 195 | + }, |
| 196 | + "custom:isVerified": { |
| 197 | + dataType: "Boolean", |
| 198 | + mutable: true, |
| 199 | + }, |
| 200 | + }, |
| 201 | +
|
| 202 | + // Multi-factor Authentication Configuration |
| 203 | + multifactor: { |
| 204 | + mode: "OPTIONAL", // Can be OPTIONAL or REQUIRED |
| 205 | + sms: true, |
| 206 | + totp: false, |
| 207 | + }, |
| 208 | +
|
| 209 | + // Account Recovery Configuration |
| 210 | + accountRecovery: "EMAIL_AND_PHONE_WITHOUT_MFA", |
| 211 | +
|
| 212 | + // Email Sender Configuration |
| 213 | + senders: { |
| 214 | + email: { |
| 215 | + |
| 216 | + }, |
| 217 | + }, |
| 218 | +}); |
| 219 | +``` |
0 commit comments