diff --git a/website/server/controllers/api-v4/auth.js b/website/server/controllers/api-v4/auth.js index 76d07d7c6c..ed20d65692 100644 --- a/website/server/controllers/api-v4/auth.js +++ b/website/server/controllers/api-v4/auth.js @@ -1,6 +1,10 @@ +import nconf from 'nconf'; import { authWithHeaders, } from '../../middlewares/auth'; +import { + NotAuthorized, +} from '../../libs/errors'; import * as authLib from '../../libs/auth'; import { model as User } from '../../models/user'; import { verifyUsername } from '../../libs/user/validation'; @@ -83,4 +87,28 @@ api.registerLocal = { }, }; +/** + * @api {put} /api/v3/user/auth/check-email Check if email is used + * @apiDescription Check if the email is already used by another user + * @apiName CheckEmail + * @apiGroup User + * + * @apiParam (Body) {String} email The checked email address. + * + * @apiSuccess {String} data.email The checked email address + */ +api.checkEmail = { + method: 'POST', + url: '/user/auth/check-email', + async handler (req, res) { + const emailAlreadyInUse = await User.findOne({ + 'auth.local.email': req.body.email.toLowerCase(), + }).select({ _id: 1 }).lean().exec(); + + if (emailAlreadyInUse) throw new NotAuthorized(res.t('emailTaken')); + + return res.respond(200, { email: req.body.email }); + }, +}; + export default api;