@@ -6,6 +6,7 @@ import fs from 'fs-extra'
66import fsStore from 'fs-blob-store'
77import mongo from 'mongodb'
88import { feathers } from '@feathersjs/feathers'
9+ import { AuthenticationService , JWTStrategy , authenticate } from '@feathersjs/authentication'
910import errors from '@feathersjs/errors'
1011import socketio from '@feathersjs/socketio'
1112import { MemoryService } from '@feathersjs/memory'
@@ -240,7 +241,7 @@ function createTests (servicePath, feathersHook, options = {}) {
240241}
241242
242243describe ( 'krawler:hooks:feathers' , ( ) => {
243- let mongoClient , app , server
244+ let mongoClient , app , server , accessToken
244245
245246 before ( async ( ) => {
246247 chailint ( chai , util )
@@ -250,12 +251,38 @@ describe('krawler:hooks:feathers', () => {
250251 await Model . createIndex ( { id : 1 } , { unique : true } )
251252 app = feathers ( )
252253 . configure ( socketio ( { path : '/ws' } ) )
254+ . use ( 'users' , new MemoryService ( { store : { 1 : { name : 'Jack Doe' , id : 1 } } , startId : 1 } ) )
253255 . use ( 'geojson-memory' , new CustomMemoryService ( { multi : true } ) , { methods : [ 'find' , 'get' , 'create' , 'update' , 'patch' , 'remove' , 'custom' ] } )
254256 . use ( 'geojson-mongodb' , new CustomMongoDBService ( { multi : true , Model } ) , { methods : [ 'find' , 'get' , 'create' , 'update' , 'patch' , 'remove' , 'custom' ] } )
257+ app . set ( 'authentication' , {
258+ secret : '1234' ,
259+ entity : 'user' ,
260+ service : 'users' ,
261+ entityId : 'id' ,
262+ authStrategies : [ 'jwt' ] ,
263+ jwtOptions : {
264+ header : { typ : 'access' } ,
265+ audience : 'https://yourdomain.com' ,
266+ issuer : 'feathers' ,
267+ algorithm : 'HS256' ,
268+ expiresIn : '1d'
269+ }
270+ } )
271+ const authService = new AuthenticationService ( app )
272+ authService . register ( 'jwt' , new JWTStrategy ( ) )
273+ app . use ( 'api/authentication' , authService )
274+ feathersOptions . authentication . accessToken = await authService . createAccessToken ( { } , { subject : '1' } )
275+ // Add authentication hooks on services
276+ app . service ( 'geojson-memory' ) . hooks ( {
277+ before : {
278+ all : authenticate ( 'jwt' )
279+ }
280+ } )
255281 // Add required hook to manage upsert
256- // Also a hook to simulate an error
282+ // Also a hook to simulate an error
257283 app . service ( 'geojson-mongodb' ) . hooks ( {
258284 before : {
285+ all : authenticate ( 'jwt' ) ,
259286 patch : [
260287 ( hook ) => { _ . set ( hook , 'params.mongodb' , { upsert : _ . get ( hook , 'params.query.upsert' , false ) } ) } ,
261288 ( hook ) => { if ( hook . data . properties === null ) throw new BadRequest ( 'Properties cannot be null' ) }
@@ -272,7 +299,11 @@ describe('krawler:hooks:feathers', () => {
272299 customMethods : [ {
273300 servicePath : 'geojson-memory' , methods : [ 'custom' ] } , {
274301 servicePath : 'geojson-mongodb' , methods : [ 'custom' ]
275- } ]
302+ } ] ,
303+ authentication : {
304+ path : 'api/authentication' ,
305+ strategy : 'jwt'
306+ }
276307 }
277308
278309 const feathersHook = {
0 commit comments