Completed
Push — master ( b6e14c...5630cf )
by
unknown
53s
created

auth-info-checker.js ➔ ???   A

Complexity

Conditions 1
Paths 18

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1.0007

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 18
dl 0
loc 14
ccs 10
cts 11
cp 0.9091
crap 1.0007
rs 9.4285
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A auth-info-checker.js ➔ ... ➔ ??? 0 9 4
1
/**
2
 * Hapi middleware for checking authentication info
3
 *
4
 * @since 1.0.0
5
 */
6
7 1
const URL_CHANGE_PASSWORD = 'change-password';
8
9 1
const defaultOptions = Object.freeze({
10
  excludeUrlPatterns: [],
11
});
12
13 1
const register = (server, opts, next) => {
14 1
  const options = Object.assign({}, defaultOptions, opts);
15 1
  options.excludeUrlPatterns.push(new RegExp(`/${URL_CHANGE_PASSWORD}`));
16 1
  server.ext('onPostAuth', (request, reply) => {
17 13
    const isApi = request.path.includes(options.apiPrefix);
18 13
    const isExcludedUrl = options.excludeUrlPatterns.some(pattern => pattern.test(request.path));
19 13
    const isTemporaryUser = request.auth && request.auth.credentials && request.auth.credentials.isTemporary;
20 13
    if (!isApi && !isExcludedUrl && isTemporaryUser) {
21
      return reply.redirect(`/${URL_CHANGE_PASSWORD}?redirect=${request.path || '/'}`);
22
    }
23 13
    return reply.continue();
24
  });
25 1
  next();
26
};
27
28 1
register.attributes = {
29
  name: 'hapi-auth-info-checker',
30
  version: '1.0.0',
31
};
32
33
module.exports = register;
34