Skip to content

Commit

Permalink
Migrated authentication.acceptInvitation method to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
naz committed Jul 25, 2019
1 parent 4da03a3 commit f4b97d3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
19 changes: 18 additions & 1 deletion core/server/api/v2/authentication.js
@@ -1,6 +1,7 @@
const auth = require('../../services/auth');
const api = require('./index');
const web = require('../../web');
const auth = require('../../services/auth');
const invitations = require('../../services/invitations');

module.exports = {
docName: 'authentication',
Expand Down Expand Up @@ -56,5 +57,21 @@ module.exports = {
});
});
}
},

acceptInvitation: {
validation: {
docName: 'invitations'
},
permissions: false,
query(frame) {
return Promise.resolve()
.then(() => {
return auth.setup.assertSetupCompleted(true);
})
.then(() => {
return invitations.accept(frame.data);
});
}
}
};
14 changes: 14 additions & 0 deletions core/server/api/v2/utils/serializers/output/authentication.js
@@ -0,0 +1,14 @@
const common = require('../../../../../lib/common');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:output:authentication');

module.exports = {
acceptInvitation(data, apiConfig, frame) {
debug('acceptInvitation');

frame.response = {
invitation: [
{message: common.i18n.t('common.api.authentication.mail.invitationAccepted')}
]
};
}
};
28 changes: 28 additions & 0 deletions core/server/api/v2/utils/validators/input/invitations.js
@@ -0,0 +1,28 @@
const Promise = require('bluebird');
const debug = require('ghost-ignition').debug('api:v2:utils:validators:input:invitation');
const common = require('../../../../../lib/common');

module.exports = {
acceptInvitation(apiConfig, frame) {
debug('acceptInvitation');

const data = frame.data.invitation[0];

if (!data.token) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noTokenProvided')}));
}

if (!data.email) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noEmailProvided')}));
}

if (!data.password) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noPasswordProvided')}));
}

if (!data.name) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noNameProvided')}));
}
}
};

2 changes: 1 addition & 1 deletion core/server/web/api/v2/admin/routes.js
Expand Up @@ -187,7 +187,7 @@ module.exports = function apiRoutes() {
api.http(apiv2.authentication.generateResetToken)
);
router.put('/authentication/passwordreset', shared.middlewares.brute.globalBlock, api.http(apiv2.authentication.resetPassword));
router.post('/authentication/invitation', api.http(api.authentication.acceptInvitation));
router.post('/authentication/invitation', api.http(apiv2.authentication.acceptInvitation));
router.get('/authentication/invitation', api.http(api.authentication.isInvitation));
router.post('/authentication/setup', api.http(api.authentication.setup));
router.put('/authentication/setup', mw.authAdminApi, api.http(api.authentication.updateSetup));
Expand Down
5 changes: 4 additions & 1 deletion core/test/regression/api/v2/admin/authentication_spec.js
Expand Up @@ -426,7 +426,10 @@ describe.only('Authentication API v2', function () {
}]
})
.expect('Content-Type', /json/)
.expect(200);
.expect(200)
.then((res) => {
res.body.invitation[0].message.should.equal('Invitation accepted.');
});
});
});
});

0 comments on commit f4b97d3

Please sign in to comment.