Total Complexity | 6 |
Complexity/F | 2 |
Lines of Code | 27 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | /** |
||
7 | 4 | const URL_CHANGE_PASSWORD = 'change-password'; |
|
8 | |||
9 | 4 | const defaultOptions = Object.freeze({ |
|
10 | excludeUrlPatterns: [], |
||
11 | }); |
||
12 | |||
13 | 4 | const register = (server, opts, next) => { |
|
14 | 4 | const options = Object.assign({}, defaultOptions, opts); |
|
15 | 4 | options.excludeUrlPatterns.push(new RegExp(`/${URL_CHANGE_PASSWORD}`)); |
|
16 | 4 | server.ext('onPostAuth', (request, reply) => { |
|
17 | 33 | const isApi = request.path.includes(options.apiPrefix); |
|
18 | 33 | const isExcludedUrl = options.excludeUrlPatterns.some(pattern => pattern.test(request.path)); |
|
19 | 33 | const isTemporaryUser = request.auth && request.auth.credentials && request.auth.credentials.isTemporary; |
|
20 | 33 | if (!isApi && !isExcludedUrl && isTemporaryUser) { |
|
21 | 2 | return reply.redirect(`/${URL_CHANGE_PASSWORD}?redirect=${request.path || '/'}`); |
|
22 | } |
||
23 | 33 | return reply.continue(); |
|
24 | }); |
||
25 | 4 | next(); |
|
26 | }; |
||
27 | |||
28 | 4 | register.attributes = { |
|
29 | name: 'hapi-auth-info-checker', |
||
30 | version: '1.0.0', |
||
31 | }; |
||
32 | |||
33 | module.exports = register; |
||
34 |