Skip to content

Commit 9cf3cd9

Browse files
committed
fix: use modern hook syntax in CLI-created app
1 parent bc25a40 commit 9cf3cd9

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

adminforth/commands/createApp/templates/adminuser.ts.hbs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import AdminForth, { AdminForthDataTypes, AdminForthResourceInput } from 'adminforth';
1+
import AdminForth, { AdminForthDataTypes, AdminForthResourceInput, AdminForthResource, AdminUser } from 'adminforth';
2+
23
import type { AdminUser } from 'adminforth';
34

45
async function canModifyUsers({ adminUser }: { adminUser: AdminUser }): Promise<boolean> {
@@ -86,18 +87,19 @@ export default {
8687
],
8788
hooks: {
8889
create: {
89-
beforeSave: async ({ record, adminUser, resource }) => {
90-
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
90+
beforeSave: async ({ record, adminUser, resource }: { record: any, adminUser: AdminUser, resource: AdminForthResource }) => {
91+
record.passwordHash = await AdminForth.Utils.generatePasswordHash(record.password);
9192
return { ok: true };
9293
}
9394
},
9495
edit: {
95-
beforeSave: async ({ record, adminUser, resource }) => {
96-
if (record.password) {
97-
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
96+
beforeSave: async ({ updates, adminUser, resource }: { updates: any, adminUser: AdminUser, resource: AdminForthResource }) => {
97+
console.log('Updating user', updates);
98+
if (updates.password) {
99+
updates.passwordHash = await AdminForth.Utils.generatePasswordHash(updates.password);
98100
}
99101
return { ok: true }
100102
},
101103
},
102-
}
104+
},
103105
} as AdminForthResourceInput;

adminforth/documentation/docs/tutorial/03-Customization/03-virtualColumns.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ Sometimes you need to visualize custom columns which do not exist in database.
77
For doing this you can use `virtual` columns.
88

99
```ts title='./resources/apartments.ts'
10-
//diff-add
11-
import { AdminForthDataTypes, AdminForthResourcePages } from 'adminforth';
1210

1311
...
1412
resourceId: 'aparts',
@@ -71,11 +69,11 @@ columns: [
7169
return iso?.toUpperCase()?.replace(/./g, (char) => String.fromCodePoint(char.charCodeAt(0) + 127397));
7270
}
7371
</script>
74-
```
72+
```
73+
74+
Here is how it looks:
7575
76-
To test component open some apartment for edit and change `description` field to `US New York`.
77-
Here is how it looks:
78-
![alt text](<Virtual columns.png>)
76+
![alt text](<Virtual columns.png>)
7977
8078
8179
## Virtual columns for editing.
@@ -108,43 +106,46 @@ columns: [
108106
]
109107
```
110108
111-
Now to handle virtual `password` field we use hooks:
109+
Now to handle virtual `password` field we use hooks:
112110
113111
114-
```ts title="./index.ts"
115-
hooks: {
112+
```ts title="./index.ts"
113+
hooks: {
116114
create: {
117-
beforeSave: async ({ record, adminUser, resource }) => {
118-
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
119-
return { ok:true, error: false };
115+
beforeSave: async ({ record, adminUser, resource }: { record: any, adminUser: AdminUser, resource: AdminForthResource }) => {
116+
record.passwordHash = await AdminForth.Utils.generatePasswordHash(record.password);
117+
return { ok: true };
120118
}
121119
},
122120
edit: {
123-
beforeSave: async ({ record, adminUser, resource}) => {
124-
if (record.password) {
125-
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
121+
beforeSave: async ({ updates, adminUser, resource }: { updates: any, adminUser: AdminUser, resource: AdminForthResource }) => {
122+
if (updates.password) {
123+
updates.passwordHash = await AdminForth.Utils.generatePasswordHash(updates.password);
126124
}
127-
return { ok: true, error: false }
125+
return { ok: true }
128126
},
129127
},
130-
}
128+
},
131129
```
132130
133-
Hook still has access to the virtual field `record.password`, and we use built-in AdminForth hasher to hash password and write it into
131+
Hook still has access to the virtual field `updates.password`, and we use built-in AdminForth hasher to hash password and write it into
134132
`password_hash` field which exists in database.
135133
136-
After hook is executed, `record.password` will be removed from the record since it is virtual, so password itself will not be saved to the database.
134+
After hook is executed, `updates.password` will be removed from the record since it is virtual, so password itself will not be saved to the database.
135+
136+
137+
### Backend-only fields
137138
138139
Another important point is that `hashed_password` field should never be passed to frontend due to security reasons.
139140
140141
To do it we have 2 options:
141142
142143
1) Do not list `password_hash` in the `columns` array of the resource. If AdminForth knows nothing about field
143144
it will never pass this field to frontend.
144-
2) Define `password_hash` but set `backendOnly`
145+
2) Define `password_hash` in columns way but set `backendOnly`. The scond option is more explicit and should be preferrred
145146
146147
```ts
147-
{
148+
{
148149
name: 'password_hash',
149150
type: AdminForthDataTypes.STRING,
150151
showIn: { all: false },

0 commit comments

Comments
 (0)