Skip to content

Commit 1290879

Browse files
authored
Merge pull request #239 from devforth/email-invite
docs: add Email Invite plugin documentation with installation and usa…
2 parents ca5c389 + 68b7d81 commit 1290879

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
44+
{ name: 'role' },
45+
46+
{
47+
name: 'password',
48+
virtual: true,
49+
required: { create: true },
50+
editingNote: { edit: 'Leave empty to keep password unchanged' },
51+
minLength: 8,
52+
type: AdminForthDataTypes.STRING,
53+
showIn: {
54+
// hide password column - but don't remove whole column it because it has constrains for password field!
55+
// diff-remove
56+
show: false,
57+
// diff-remove
58+
list: false,
59+
// diff-remove
60+
filter: false,
61+
// diff-add
62+
all: false,
63+
},
64+
masked: true,
65+
},
66+
67+
// ... other columns
68+
],
69+
hooks: {
70+
create: {
71+
beforeSave: async ({ record, adminUser, resource }: { record: any, adminUser: AdminUser, resource: AdminForthResource }) => {
72+
// since we don't show password input in resource - no sense to hande it in hook anymore!
73+
//diff-remove
74+
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
75+
return { ok: true };
76+
}
77+
},
78+
edit: {
79+
beforeSave: async ({ oldRecord, updates, adminUser, resource }: { oldRecord: any, updates: any, adminUser: AdminUser, resource: AdminForthResource }) => {
80+
console.log('Updating user', updates);
81+
if (oldRecord.id === adminUser.dbUser.id && updates.role) {
82+
return { ok: false, error: 'You cannot change your own role' };
83+
}
84+
// also no sense to have updatres - we dont allow edit password by admin anymore
85+
//diff-remove
86+
if (updates.password) {
87+
//diff-remove
88+
updates.password_hash = await AdminForth.Utils.generatePasswordHash(updates.password);
89+
//diff-remove
90+
}
91+
return { ok: true }
92+
},
93+
},
94+
},
95+
plugins: [
96+
new EmailInvitePlugin({
97+
emailField: 'email',
98+
sendFrom: '[email protected]',
99+
adapter: new EmailAdapterAwsSes({
100+
region: 'us-east-1',
101+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
102+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
103+
}),
104+
}),
105+
],
106+
};
107+
```
108+
109+
Please note that previously (in defauklt CLI setup) we needed it to allow admins to set passwords when created new users (to invite them). Also Admens were able to edit passwords of users.
110+
Now since we added this plugin, user will have email link on which he will get form to enter password by hiumself.
111+
Please note that the form for user will still use constraints from password virtual field, that is why we just hid it using showIn - not remove it.
112+
113+
To allow users to edit their passwords please use [email password reset plugin](https://adminforth.dev/docs/tutorial/Plugins/email-password-reset/)
114+
115+
## Email Confirmation boolean flag
116+
117+
This plugin can write into the database the fact that invited user was able to set password and as a result confirmed that he owns his email.
118+
To enable email this behaviour, first add a boolean field to your user table:
119+
120+
```prisma title="./schema.prisma"
121+
model adminuser {
122+
id String @id @default(cuid())
123+
email String @unique
124+
password_hash String
125+
role String @default("user")
126+
//diff-add
127+
email_confirmed Boolean? @default(false)
128+
// ... other fields
129+
}
130+
```
131+
132+
Run the migration:
133+
134+
```bash
135+
npx prisma migrate dev --name add-email-confirmed
136+
```
137+
138+
Then update your resource configuration:
139+
140+
```typescript title="./resources/adminuser.ts"
141+
export default {
142+
// ... existing config
143+
columns: [
144+
{ name: 'id', primaryKey: true },
145+
{ name: 'email', required: true },
146+
{ name: 'password_hash', showIn: [] },
147+
{ name: 'role' },
148+
//diff-add
149+
{ name: 'email_confirmed', type: AdminForthDataTypes.BOOLEAN },
150+
// ... other columns
151+
],
152+
plugins: [
153+
new EmailInvitePlugin({
154+
emailField: 'email',
155+
sendFrom: '[email protected]',
156+
adapter: new EmailAdapterAwsSes(/* ... */),
157+
//diff-add
158+
emailConfirmedField: 'email_confirmed', // Enable email confirmation
159+
}),
160+
],
161+
};
162+
```
163+

0 commit comments

Comments
 (0)