Skip to content

Commit 899a08d

Browse files
committed
docs: about custom layout and redirects, fix missing hook in hello wowrld cli
1 parent 90aab88 commit 899a08d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

adminforth/documentation/docs/tutorial/01-helloWorld.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ Create `index.ts` file in root directory with following content:
131131

132132
```ts title="./index.ts"
133133
import express from 'express';
134-
import AdminForth, { AdminForthDataTypes, AdminUser, Filters } from 'adminforth';
134+
import AdminForth, { AdminForthDataTypes, Filters } from 'adminforth';
135+
import type { AdminForthResourceInput, AdminForthResource, AdminUser } from 'adminforth';
135136

136137
export const admin = new AdminForth({
137138
baseUrl: '',
@@ -208,6 +209,26 @@ export const admin = new AdminForth({
208209
},
209210
{ name: 'passwordHash', backendOnly: true, showIn: { all: false } }
210211
],
212+
hooks: {
213+
create: {
214+
beforeSave: async ({ record, adminUser, resource }: { record: any, adminUser: AdminUser, resource: AdminForthResource }) => {
215+
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
216+
return { ok: true };
217+
}
218+
},
219+
edit: {
220+
beforeSave: async ({ oldRecord, updates, adminUser, resource }: { oldRecord: any, updates: any, adminUser: AdminUser, resource: AdminForthResource }) => {
221+
console.log('Updating user', updates);
222+
if (oldRecord.id === adminUser.dbUser.id && updates.role) {
223+
return { ok: false, error: 'You cannot change your own role' };
224+
}
225+
if (updates.password) {
226+
updates.password_hash = await AdminForth.Utils.generatePasswordHash(updates.password);
227+
}
228+
return { ok: true }
229+
},
230+
},
231+
}
211232
},
212233
{
213234
table: 'post',

adminforth/documentation/docs/tutorial/03-Customization/06-customPages.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,18 @@ You can navigate user to this page using any router link, e.g.:
457457
</template>
458458
```
459459
460+
Add to your `<script setup>` section:
461+
460462
```ts
461463
import { Link } from '@/afcl';
462464
```
463465
466+
If you set `customLayout: true` in the `meta` object, it will not include default layout like sidebar and header, so you can create your own layout for this page.
467+
468+
> **Redirects to login page**: Any route which has sidebar and header (e.g. default CRUD pages or menu item with `component`) uses internal AdminForth REST API to fetch menu items and user information, so it passes authentication check and if authentication cookie is not provided or has expired JWT user gets redirected to the login page.
469+
> In case if you set `customLayout: true`, it will not call these APIs so user will not be automatically redirected to the login page in case of expired or not-provided authentication cookie. That feature allows you to implement public pages without authentication, e.g. Terms of Service, Privacy Policy and many others. In case if you need to check if user is logged in just call any custom API which has `admin.express.authorize` middleware. Obviously for public pages you should create API endpoint WITHOUT `admin.express.authorize` middleware.
470+
471+
464472
### Passing meta attributes to the page
465473
466474

0 commit comments

Comments
 (0)