From e99b51895afd532a529744396ecae87d47c68503 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 21 Jun 2020 14:29:59 +0300 Subject: feat: change hooks and install which-types --- services/auth/auth.service.ts | 2 +- services/polls/polls.hooks.ts | 10 +++++----- services/profiles/profiles.hooks.ts | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'services') diff --git a/services/auth/auth.service.ts b/services/auth/auth.service.ts index 42846b0..826357c 100644 --- a/services/auth/auth.service.ts +++ b/services/auth/auth.service.ts @@ -1,6 +1,6 @@ import { AuthenticationService, - JWTStrategy + JWTStrategy } from '@feathersjs/authentication'; import { LocalStrategy } from '@feathersjs/authentication-local'; import { Application } from '@feathersjs/express'; diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index 0637320..a914cd0 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,12 +1,12 @@ import { - expandAuthorHook, - expandAuthorManyHook -} from '../../hooks/expandAuthor'; + convertPollHook, + convertPollManyHook +} from '../../hooks/convertPoll'; export default { after: { - get: [expandAuthorHook], - find: [expandAuthorManyHook] + get: [convertPollHook], + find: [convertPollManyHook] } }; diff --git a/services/profiles/profiles.hooks.ts b/services/profiles/profiles.hooks.ts index bb05d94..4447bee 100644 --- a/services/profiles/profiles.hooks.ts +++ b/services/profiles/profiles.hooks.ts @@ -1,10 +1,10 @@ import { - expandAuthorManyHook -} from '../../hooks/expandAuthor'; + convertPollManyHook +} from '../../hooks/convertPoll'; export default { after: { - get: [expandAuthorManyHook] + get: [convertPollManyHook] } }; -- cgit v1.2.3 From 4909dda29566710793b8445ab4426102ca4a0324 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 01:42:43 +0300 Subject: feat: create Votes service --- services/index.ts | 2 ++ services/votes/votes.class.ts | 14 ++++++++++++++ services/votes/votes.hooks.ts | 15 +++++++++++++++ services/votes/votes.service.ts | 10 ++++++++++ 4 files changed, 41 insertions(+) create mode 100644 services/votes/votes.class.ts create mode 100644 services/votes/votes.hooks.ts create mode 100644 services/votes/votes.service.ts (limited to 'services') diff --git a/services/index.ts b/services/index.ts index f000837..638fb7a 100644 --- a/services/index.ts +++ b/services/index.ts @@ -2,6 +2,7 @@ import { Application } from '@feathersjs/express'; import Users from './users/users.service'; import Polls from './polls/polls.service'; import Profiles from './profiles/profiles.service'; +import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; export default (app: Application): void => { @@ -9,5 +10,6 @@ export default (app: Application): void => { app.configure(Users); app.configure(Polls); app.configure(Profiles); + app.configure(Votes); }; diff --git a/services/votes/votes.class.ts b/services/votes/votes.class.ts new file mode 100644 index 0000000..d95c148 --- /dev/null +++ b/services/votes/votes.class.ts @@ -0,0 +1,14 @@ +import PollModel from '../../models/polls/poll.model'; +import { PollSchema } from '../../models/polls/poll.schema'; + +export default class Votes { + async create(data: any, params: any): Promise { + return PollModel.findById(params.route.id) + .then(poll => poll?.vote(params.user._id, data.which)) + .catch(e => { + console.error(e); + return null; + }); + } +} + diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts new file mode 100644 index 0000000..2e29008 --- /dev/null +++ b/services/votes/votes.hooks.ts @@ -0,0 +1,15 @@ +import { + convertPollHook +} from '../../hooks/convertPoll'; + +import { authenticate } from '@feathersjs/authentication'; + +export default { + before: { + create: [authenticate('jwt')] + }, + after: { + all: [convertPollHook] + } +}; + diff --git a/services/votes/votes.service.ts b/services/votes/votes.service.ts new file mode 100644 index 0000000..3947d9b --- /dev/null +++ b/services/votes/votes.service.ts @@ -0,0 +1,10 @@ +import { Application } from '@feathersjs/express'; +import Votes from './votes.class'; + +import hooks from './votes.hooks'; + +export default (app: Application): void => { + app.use('/polls/:id/votes/', new Votes()); + app.service('/polls/:id/votes/').hooks(hooks); +}; + -- cgit v1.2.3 From ea35b242da316e75928a0a6d336378ea50f4d6f8 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 19:08:35 +0300 Subject: fix: protect password field in ALL calls --- services/users/users.hooks.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'services') diff --git a/services/users/users.hooks.ts b/services/users/users.hooks.ts index fc17ed7..580d1d2 100644 --- a/services/users/users.hooks.ts +++ b/services/users/users.hooks.ts @@ -1,7 +1,14 @@ import { hooks } from '@feathersjs/authentication-local'; +import { HookContext } from '@feathersjs/feathers'; const hashPassword = hooks.hashPassword('password'); -const protectPassword = hooks.protect('password'); + +const protectPassword = async (context: HookContext): Promise => { + const { dispatch } = hooks.protect('password')(context); + context.result = dispatch; + context.dispatch = dispatch; + return context; +} export default { after: { -- cgit v1.2.3 From ef94c648b14bd09bf28b8f47ed015b319aa8f0cc Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 21:09:14 +0300 Subject: fix: protect only local 'get' queries --- services/users/users.hooks.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'services') diff --git a/services/users/users.hooks.ts b/services/users/users.hooks.ts index 580d1d2..8eecca3 100644 --- a/services/users/users.hooks.ts +++ b/services/users/users.hooks.ts @@ -3,16 +3,15 @@ import { HookContext } from '@feathersjs/feathers'; const hashPassword = hooks.hashPassword('password'); -const protectPassword = async (context: HookContext): Promise => { - const { dispatch } = hooks.protect('password')(context); - context.result = dispatch; - context.dispatch = dispatch; +const localDispatch = async (context: HookContext): Promise => { + context.result = context.dispatch; return context; } export default { after: { - all: [protectPassword] + all: [hooks.protect('password')], + get: [localDispatch] // Protect password from local get's }, before: { create: [hashPassword], -- cgit v1.2.3 From 967ee2225a874dc114079882d737a25a487404be Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 21:11:19 +0300 Subject: refactor: improve vote endpoint --- services/votes/votes.class.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'services') diff --git a/services/votes/votes.class.ts b/services/votes/votes.class.ts index d95c148..0490b7e 100644 --- a/services/votes/votes.class.ts +++ b/services/votes/votes.class.ts @@ -3,12 +3,14 @@ import { PollSchema } from '../../models/polls/poll.schema'; export default class Votes { async create(data: any, params: any): Promise { - return PollModel.findById(params.route.id) - .then(poll => poll?.vote(params.user._id, data.which)) - .catch(e => { - console.error(e); - return null; - }); + const poll = await PollModel.findById(params.route.id); + if (poll) { + const which: 'left' | 'right' = data.which; + const { user } = params; + poll.contents[which].votes.push(user._id); + return poll.save(); + } + return null; } } -- cgit v1.2.3 From 676c722a994a214c182bcf26b80eab09ebf9f61e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 21:29:17 +0300 Subject: refactor: unite convertPoll hooks --- services/polls/polls.hooks.ts | 8 ++------ services/profiles/profiles.hooks.ts | 6 ++---- services/votes/votes.hooks.ts | 7 ++----- 3 files changed, 6 insertions(+), 15 deletions(-) (limited to 'services') diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index a914cd0..eba3e63 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,12 +1,8 @@ -import { - convertPollHook, - convertPollManyHook -} from '../../hooks/convertPoll'; +import convertPoll from '../../hooks/convertPoll'; export default { after: { - get: [convertPollHook], - find: [convertPollManyHook] + all: [convertPoll], } }; diff --git a/services/profiles/profiles.hooks.ts b/services/profiles/profiles.hooks.ts index 4447bee..13d6f63 100644 --- a/services/profiles/profiles.hooks.ts +++ b/services/profiles/profiles.hooks.ts @@ -1,10 +1,8 @@ -import { - convertPollManyHook -} from '../../hooks/convertPoll'; +import convertPoll from '../../hooks/convertPoll'; export default { after: { - get: [convertPollManyHook] + all: [convertPoll] } }; diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts index 2e29008..a41b8a9 100644 --- a/services/votes/votes.hooks.ts +++ b/services/votes/votes.hooks.ts @@ -1,15 +1,12 @@ -import { - convertPollHook -} from '../../hooks/convertPoll'; - import { authenticate } from '@feathersjs/authentication'; +import convertPoll from '../../hooks/convertPoll'; export default { before: { create: [authenticate('jwt')] }, after: { - all: [convertPollHook] + all: [convertPoll] } }; -- cgit v1.2.3 From 5255b85532f2e2709b31e891a167c1a250a0aa7e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 23 Jun 2020 02:33:10 +0300 Subject: fix: return object instead of document --- services/votes/votes.class.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'services') diff --git a/services/votes/votes.class.ts b/services/votes/votes.class.ts index 0490b7e..6b9181b 100644 --- a/services/votes/votes.class.ts +++ b/services/votes/votes.class.ts @@ -8,7 +8,8 @@ export default class Votes { const which: 'left' | 'right' = data.which; const { user } = params; poll.contents[which].votes.push(user._id); - return poll.save(); + poll.save(); + return poll.toObject(); } return null; } -- cgit v1.2.3 From dbfb0d30f0e5cd44ea188d72ceb05acac2ac40d7 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 23 Jun 2020 22:50:59 +0300 Subject: feat: only allow voting once --- services/votes/votes.class.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'services') diff --git a/services/votes/votes.class.ts b/services/votes/votes.class.ts index 6b9181b..3220ee7 100644 --- a/services/votes/votes.class.ts +++ b/services/votes/votes.class.ts @@ -5,11 +5,8 @@ export default class Votes { async create(data: any, params: any): Promise { const poll = await PollModel.findById(params.route.id); if (poll) { - const which: 'left' | 'right' = data.which; - const { user } = params; - poll.contents[which].votes.push(user._id); - poll.save(); - return poll.toObject(); + const updatedPoll = await poll.vote(params.user._id, data.which); + return updatedPoll.toObject(); } return null; } -- cgit v1.2.3 From fc9cf3b8f77e9068fa51d614b0d0b120f7bf2440 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 09:03:46 +0300 Subject: feat!: construct VoteService based on VoteModel --- services/votes/votes.class.ts | 14 -------------- services/votes/votes.service.ts | 9 ++++++--- 2 files changed, 6 insertions(+), 17 deletions(-) delete mode 100644 services/votes/votes.class.ts (limited to 'services') diff --git a/services/votes/votes.class.ts b/services/votes/votes.class.ts deleted file mode 100644 index 3220ee7..0000000 --- a/services/votes/votes.class.ts +++ /dev/null @@ -1,14 +0,0 @@ -import PollModel from '../../models/polls/poll.model'; -import { PollSchema } from '../../models/polls/poll.schema'; - -export default class Votes { - async create(data: any, params: any): Promise { - const poll = await PollModel.findById(params.route.id); - if (poll) { - const updatedPoll = await poll.vote(params.user._id, data.which); - return updatedPoll.toObject(); - } - return null; - } -} - diff --git a/services/votes/votes.service.ts b/services/votes/votes.service.ts index 3947d9b..81f767c 100644 --- a/services/votes/votes.service.ts +++ b/services/votes/votes.service.ts @@ -1,10 +1,13 @@ import { Application } from '@feathersjs/express'; -import Votes from './votes.class'; +import service from 'feathers-mongoose'; +import Model from '../../models/votes/vote.model'; import hooks from './votes.hooks'; +const VoteService = service({ Model }); + export default (app: Application): void => { - app.use('/polls/:id/votes/', new Votes()); - app.service('/polls/:id/votes/').hooks(hooks); + app.use('/votes/', VoteService); + app.service('votes').hooks(hooks); }; -- cgit v1.2.3 From 681e51a658bfed723b368fc5b71b1350909d9496 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 11:02:54 +0300 Subject: feat: setup hooks for VoteService --- services/votes/votes.hooks.ts | 13 ++++++++----- services/votes/votes.service.ts | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'services') diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts index a41b8a9..63f19e3 100644 --- a/services/votes/votes.hooks.ts +++ b/services/votes/votes.hooks.ts @@ -1,12 +1,15 @@ +import { HookContext } from '@feathersjs/feathers'; import { authenticate } from '@feathersjs/authentication'; -import convertPoll from '../../hooks/convertPoll'; + +const addUserId = async (context: HookContext): Promise => { + const { params: { user} } = context; + context.data.userId = user._id; + return context; +}; export default { before: { - create: [authenticate('jwt')] - }, - after: { - all: [convertPoll] + create: [authenticate('jwt'), addUserId] } }; diff --git a/services/votes/votes.service.ts b/services/votes/votes.service.ts index 81f767c..cb40c1a 100644 --- a/services/votes/votes.service.ts +++ b/services/votes/votes.service.ts @@ -7,7 +7,7 @@ import hooks from './votes.hooks'; const VoteService = service({ Model }); export default (app: Application): void => { - app.use('/votes/', VoteService); + app.use('/votes', VoteService); app.service('votes').hooks(hooks); }; -- cgit v1.2.3 From 64f5f8c3f9660f649dfdaad07d84aa8c26b9661e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 12:08:15 +0300 Subject: feat: setup global auth hooks --- services/index.ts | 8 ++++++++ services/votes/votes.hooks.ts | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'services') diff --git a/services/index.ts b/services/index.ts index 638fb7a..fe5ffdc 100644 --- a/services/index.ts +++ b/services/index.ts @@ -5,11 +5,19 @@ import Profiles from './profiles/profiles.service'; import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; +import tryAuthenticate from '../hooks/tryAuthenticate'; + export default (app: Application): void => { app.configure(Auth); app.configure(Users); app.configure(Polls); app.configure(Profiles); app.configure(Votes); + + app.hooks({ + before: { + all: tryAuthenticate + } + }) }; diff --git a/services/votes/votes.hooks.ts b/services/votes/votes.hooks.ts index 63f19e3..1cf7261 100644 --- a/services/votes/votes.hooks.ts +++ b/services/votes/votes.hooks.ts @@ -1,5 +1,5 @@ import { HookContext } from '@feathersjs/feathers'; -import { authenticate } from '@feathersjs/authentication'; +import requireAuth from '../../hooks/requireAuth'; const addUserId = async (context: HookContext): Promise => { const { params: { user} } = context; @@ -9,7 +9,7 @@ const addUserId = async (context: HookContext): Promise => { export default { before: { - create: [authenticate('jwt'), addUserId] + create: [requireAuth, addUserId] } }; -- cgit v1.2.3