|
| 1 | +# Email Invite |
| 2 | + |
| 3 | +Email Invite plugin allows administrators to create users without setting passwords. Instead, the plugin sends an email invitation to the newly created user, asking them to set their own password. This is more secure and user-friendly than having administrators set passwords for users. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install the plugin: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @adminforth/email-invite --save |
| 11 | +``` |
| 12 | + |
| 13 | +You'll also need an email adapter. For AWS SES: |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install @adminforth/email-adapter-aws-ses --save |
| 17 | +``` |
| 18 | + |
| 19 | +## SES |
| 20 | + |
| 21 | +To Setup SES, you need to have an AWS account and SES service enabled. You can follow the steps below to setup SES. |
| 22 | + |
| 23 | +1. Go to the AWS Management Console and open the Amazon SES console at [https://console.aws.amazon.com/ses/](https://console.aws.amazon.com/ses/). |
| 24 | +2. Make sure you are in the correct region. You can change the region from the top right corner. For example, if you are in the `us-east-1` region, you can see the region name US East (N. Virginia) in the top right corner. |
| 25 | + |
| 26 | +3. Add your email address (any email), and verify it. |
| 27 | +4. Add some domain you own and verify it by creating DNS records which AWS suggests. This will be used as the domain for sending emails. e.g. if you want to send from [email protected] you need to verify `devforth.io`. |
| 28 | + |
| 29 | +## Basic Usage |
| 30 | + |
| 31 | +```typescript title="./resources/adminuser.ts" |
| 32 | +import EmailInvitePlugin from '@adminforth/email-invite'; |
| 33 | +import EmailAdapterAwsSes from '@adminforth/email-adapter-aws-ses'; |
| 34 | + |
| 35 | +export default { |
| 36 | + dataSource: 'maindb', |
| 37 | + table: 'adminuser', |
| 38 | + resourceId: 'adminuser', |
| 39 | + columns: [ |
| 40 | + { name: 'id', primaryKey: true }, |
| 41 | + { name: 'email', required: true }, |
| 42 | + { name: 'password_hash', showIn: [] }, // Hide from UI |
| 43 | + { name: 'role' }, |
| 44 | + // ... other columns |
| 45 | + ], |
| 46 | + plugins: [ |
| 47 | + new EmailInvitePlugin({ |
| 48 | + emailField: 'email', |
| 49 | + |
| 50 | + adapter: new EmailAdapterAwsSes({ |
| 51 | + region: 'us-east-1', |
| 52 | + accessKeyId: process.env.AWS_ACCESS_KEY_ID, |
| 53 | + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, |
| 54 | + }), |
| 55 | + }), |
| 56 | + ], |
| 57 | +}; |
| 58 | +``` |
| 59 | + |
| 60 | +## Email Confirmation |
| 61 | + |
| 62 | +To enable email confirmation, first add a boolean field to your user table: |
| 63 | + |
| 64 | +```prisma title="./schema.prisma" |
| 65 | +model adminuser { |
| 66 | + id String @id @default(cuid()) |
| 67 | + email String @unique |
| 68 | + password_hash String |
| 69 | + role String @default("user") |
| 70 | + //diff-add |
| 71 | + email_confirmed Boolean? @default(false) |
| 72 | + // ... other fields |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Run the migration: |
| 77 | + |
| 78 | +```bash |
| 79 | +npx prisma migrate dev --name add-email-confirmed |
| 80 | +``` |
| 81 | + |
| 82 | +Then update your resource configuration: |
| 83 | + |
| 84 | +```typescript title="./resources/adminuser.ts" |
| 85 | +export default { |
| 86 | + // ... existing config |
| 87 | + columns: [ |
| 88 | + { name: 'id', primaryKey: true }, |
| 89 | + { name: 'email', required: true }, |
| 90 | + { name: 'password_hash', showIn: [] }, |
| 91 | + { name: 'role' }, |
| 92 | + //diff-add |
| 93 | + { name: 'email_confirmed', type: AdminForthDataTypes.BOOLEAN }, |
| 94 | + // ... other columns |
| 95 | + ], |
| 96 | + plugins: [ |
| 97 | + new EmailInvitePlugin({ |
| 98 | + emailField: 'email', |
| 99 | + |
| 100 | + adapter: new EmailAdapterAwsSes(/* ... */), |
| 101 | + //diff-add |
| 102 | + emailConfirmedField: 'email_confirmed', // Enable email confirmation |
| 103 | + }), |
| 104 | + ], |
| 105 | +}; |
| 106 | +``` |
| 107 | + |
0 commit comments