import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication'
import { LocalStrategy } from '@feathersjs/authentication-local'
import { BadRequest } from '@feathersjs/errors'

import filterAllowedFields from '../hooks/filterAllowedFields'

class UserRolesAuthenticationService extends AuthenticationService {
  async getPayload(authResult, params) {
    const payload = await super.getPayload(authResult, params)
    const { user } = authResult
    // add roles to payload
    return Object.assign(payload, { roles: user && user.roles })
  }
}

export const restrictAuthenticationResponse = async (ctx) => {
  const {
    accessToken,
    user: { email, name, phone },
  } = ctx.result
  ctx.result = { accessToken: accessToken, user: { email, name, phone } }
  return ctx
}

export default (app) => {
  const authService = new UserRolesAuthenticationService(app)
  authService.register('jwt', new JWTStrategy())
  authService.register('local', new LocalStrategy())

  app.use('/authentication', authService)
  app
    .service('authentication')
    .hooks({
      before: {
        all: [],
        create: [],
        remove: [],
      },
      after: {
        all: [],
        create: [
          (ctx) => {
            if (!ctx.result.user || !ctx.result.user.isVerified) {
              throw new BadRequest("User's email is not yet verified.")
            }
          },
          restrictAuthenticationResponse,
        ],
        remove: [],
      },
      error: {
        all: [],
        create: [],
        remove: [],
      },
    })
    .hooks({
      after: {
        all: [filterAllowedFields],
      },
    })
}

@@ -569,38 +569,25 @@
 t const 
-restrictAuthentication
+buildAuth
 Response
@@ -663,16 +663,23 @@
 e, phone
+, roles
  %7D,%0A  %7D 
@@ -707,16 +707,20 @@
 sult = %7B
+%0A   
  accessT
@@ -737,16 +737,20 @@
 ssToken,
+%0A   
  user: %7B
@@ -768,18 +768,54 @@
 e, phone
+, roles: roles.map((r) =%3E r.name) %7D,%0A
  
-%7D
  %7D%0A  ret
@@ -1441,30 +1441,17 @@
     
-restrictAuthentication
+buildAuth
 Resp

Add roles to auth response.