
// Configure authentication. (Can be re-generated.)
<%- tplImports('authentication', '@feathersjs/authentication') %>
<%- tplImports('jwt', '@feathersjs/authentication-jwt') %>
<%# -%>
<%# --- if-1 starts below. -%>
<% if(specs.authentication.strategies.indexOf('local') !== -1) { -%>
<%- tplImports('local', '@feathersjs/authentication-local') %>
<% } -%>
<%# --- if-1 ends above. -%>
<%# -%>
<%# --- if-2 starts below. -%>
<% if(oauthProviders.length){ -%>
<%- tplImports('oauth2', '@feathersjs/authentication-oauth2') %>
<% } -%>
<%# --- if-2 ends above. -%>
<%# -%>
<%# --- forEach-3 starts below. -%>
<% oauthProviders.forEach(provider => { -%>
<%- tplImports(provider.strategyName, provider.module, 'req') %>
<% }); -%>
<%# --- forEach-3 ends above. -%>

<%- tplTsOnly([`import { App } from './app.interface'${sc}`, '']) -%>
<%- insertFragment('imports') %>
<%- insertFragment('init') %>

<%- tplModuleExports(null, 'function (app) {', 'function (app: App) {') %>
  const config = app.get('authentication')<%- sc %>
  <%- insertFragment('func_init') %>

  // Set up authentication with the secret
  app.configure(authentication(config))<%- sc %>
  app.configure(jwt())<%- sc %><% if(specs.authentication.strategies.indexOf('local') !== -1) { %>
  app.configure(local())<%- sc %><% } %>
  <%- insertFragment('loc_1') %>
<%# -%>
<%# --- forEach-1 starts below. -%>
<% oauthProviders.forEach(provider => { %>
  app.configure(oauth2(Object.assign({
    name: '<%= provider.name %>',
    Strategy: <%= provider.strategyName %>,
    <%- insertFragment(provider.name + '_options') %>
  }, config.<%= provider.name %>)))<%- sc %>
<% }); %>
<%# --- forEach-1 ends above. -%>
  <%- insertFragment('loc_2') %>

  // The `authentication` service is used to create a JWT.
  // The before `create` hook registers strategies that can be used
  // to create a new valid JWT (e.g. local or oauth2)
  app.service('authentication').hooks({
    before: {
      create: [
        <%- insertFragment('before_create', [
          '        authentication.hooks.authenticate(config.strategies),'
        ]) %>
      ],
      remove: [
        <%- insertFragment('before_remove', [
          '        authentication.hooks.authenticate(\'jwt\'),'
        ]) %>
      ],
      <%- insertFragment('before') %>
    },
    <%- insertFragment('after') %>
  })<%- sc %>
  <%- insertFragment('func_return') %>
}<%- sc %>

<%- insertFragment('exports') %>
<%- `${tplExport('moduleExports')}${sc}` %>

<%- insertFragment('funcs') %>
<%- insertFragment('end') %>
