Skip to content

Commit 68b7d81

Browse files
authored
Update 15-email-invite.md
1 parent 2a94212 commit 68b7d81

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

adminforth/documentation/docs/tutorial/05-Plugins/15-email-invite.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,58 @@ export default {
4040
{ name: 'id', primaryKey: true },
4141
{ name: 'email', required: true },
4242
{ name: 'password_hash', showIn: [] }, // Hide from UI
43+
4344
{ 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+
4467
// ... other columns
4568
],
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+
},
4695
plugins: [
4796
new EmailInvitePlugin({
4897
emailField: 'email',
@@ -57,9 +106,16 @@ export default {
57106
};
58107
```
59108

60-
## Email Confirmation
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
61116

62-
To enable email confirmation, first add a boolean field to your user table:
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:
63119

64120
```prisma title="./schema.prisma"
65121
model adminuser {

0 commit comments

Comments
 (0)