Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions docs/PLUGIN_OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ If provided, the `onRequest` plugin option will be invoked on each request to an
```ts
export type OnRequestCallback = (
request: AnyFormRequest,
params: FormParams,
definition: FormDefinition,
metadata: FormMetadata
) => void
h: FormResponseToolkit,
context: FormContext
) => ResponseObject | FormResponseToolkit['continue'] | Promise<ResponseObject | FormResponseToolkit['continue']>
```

Here's an example of how it could be used to secure access to forms:
Expand All @@ -247,13 +246,17 @@ Here's an example of how it could be used to secure access to forms:
await server.register({
plugin,
options: {
onRequest: (request , params, definition, metadata) => {
const { auth } = request
onRequest: async (request, h, context) => {
const { auth } = request

if (!auth.isAuthenticated) {
throw Boom.unauthorized()
}
// Check if user is authenticated
if (!auth.isAuthenticated) {
return h.redirect('/login').takeover()
}

// Return h.continue to resume with normal form processing
return h.continue
}
}
})
```
Expand Down
3 changes: 2 additions & 1 deletion src/server/plugins/engine/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ describe('Helpers', () => {

h = {
redirect: jest.fn().mockImplementation(() => response),
view: jest.fn()
view: jest.fn(),
continue: Symbol('continue')
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ describe('PageController', () => {

const h: FormResponseToolkit = {
redirect: jest.fn(),
view: jest.fn()
view: jest.fn(),
continue: Symbol('continue')
}

it('returns default route options', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,8 @@ describe('QuestionPageController', () => {

const h: FormResponseToolkit = {
redirect: jest.fn().mockReturnValue(response),
view: jest.fn()
view: jest.fn(),
continue: Symbol('continue')
}

it('returns default route options', () => {
Expand Down Expand Up @@ -1373,7 +1374,8 @@ describe('QuestionPageController V2', () => {

const h: FormResponseToolkit = {
redirect: jest.fn().mockReturnValue(response),
view: jest.fn()
view: jest.fn(),
continue: Symbol('continue')
}

it('returns default route options', () => {
Expand Down Expand Up @@ -1532,7 +1534,8 @@ describe('Save and Exit functionality', () => {

const h: FormResponseToolkit = {
redirect: jest.fn().mockReturnValue(response),
view: jest.fn()
view: jest.fn(),
continue: Symbol('continue')
}

beforeEach(() => {
Expand Down Expand Up @@ -1663,7 +1666,8 @@ describe('Save and Exit functionality', () => {

const mockH = {
redirect: jest.fn().mockReturnValue(mockResponse),
view: jest.fn()
view: jest.fn(),
continue: Symbol('continue')
}

const postHandler = controller1.makePostRouteHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('SummaryPageController', () => {
}
const h: FormResponseToolkit = {
redirect: jest.fn().mockReturnValue(response),
view: jest.fn()
view: jest.fn(),
continue: Symbol('continue')
}

beforeEach(() => {
Expand Down
14 changes: 10 additions & 4 deletions src/server/plugins/engine/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const plugin = {
saveAndExit,
nunjucks: nunjucksOptions,
viewContext,
preparePageEventRequestOptions
preparePageEventRequestOptions,
onRequest
} = options

const cacheService =
Expand Down Expand Up @@ -83,10 +84,15 @@ export const plugin = {
...getQuestionRoutes(
getRouteOptions,
postRouteOptions,
preparePageEventRequestOptions
preparePageEventRequestOptions,
onRequest
),
...getRepeaterSummaryRoutes(getRouteOptions, postRouteOptions, onRequest),
...getRepeaterItemDeleteRoutes(
getRouteOptions,
postRouteOptions,
onRequest
),
...getRepeaterSummaryRoutes(getRouteOptions, postRouteOptions),
...getRepeaterItemDeleteRoutes(getRouteOptions, postRouteOptions),
...getFileUploadStatusRoutes()
]

Expand Down
Loading
Loading