2020-11-20 10:30:12 +00:00
|
|
|
const express = require('express');
|
2020-10-20 19:25:00 +00:00
|
|
|
const router = express.Router();
|
|
|
|
const Joi = require('joi');
|
2020-11-20 10:30:12 +00:00
|
|
|
const wrapRoute = require('../_helpers/wrapRoute');
|
2020-10-20 19:25:00 +00:00
|
|
|
const validateRequest = require('../_middleware/validate-request');
|
|
|
|
const accountService = require('./account.service');
|
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
router.post(
|
|
|
|
'/register',
|
|
|
|
validateRequest(
|
|
|
|
Joi.object({
|
|
|
|
username: Joi.string().required(),
|
|
|
|
email: Joi.string().email().required(),
|
|
|
|
password: Joi.string().min(6).required(),
|
|
|
|
confirmPassword: Joi.string().valid(Joi.ref('password')).required(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
await accountService.register(req.body, req.get('origin'));
|
|
|
|
res.json({ message: 'Registration successful, please check your email for verification instructions' });
|
|
|
|
}),
|
|
|
|
);
|
2020-10-20 19:25:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
router.post(
|
|
|
|
'/verify-email',
|
|
|
|
validateRequest(
|
|
|
|
Joi.object({
|
|
|
|
token: Joi.string().required(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
await accountService.verifyEmail(req.body);
|
|
|
|
res.json({ message: 'Verification successful, you can now login' });
|
|
|
|
}),
|
|
|
|
);
|
2020-10-20 19:25:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
router.post(
|
|
|
|
'/forgot-password',
|
|
|
|
validateRequest(
|
|
|
|
Joi.object({
|
|
|
|
email: Joi.string().email().required(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
await accountService.forgotPassword(req.body, req.get('origin'));
|
|
|
|
res.json({ message: 'Please check your email for password reset instructions' });
|
|
|
|
}),
|
|
|
|
);
|
2020-10-20 19:25:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
router.post(
|
|
|
|
'/validate-reset-token',
|
|
|
|
validateRequest(
|
|
|
|
Joi.object({
|
|
|
|
token: Joi.string().required(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
await accountService.validateResetToken(req.body);
|
|
|
|
res.json({ message: 'Token is valid' });
|
|
|
|
}),
|
|
|
|
);
|
2020-10-20 19:25:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
router.post(
|
|
|
|
'/reset-password',
|
|
|
|
validateRequest(
|
|
|
|
Joi.object({
|
|
|
|
token: Joi.string().required(),
|
|
|
|
password: Joi.string().min(6).required(),
|
|
|
|
confirmPassword: Joi.string().valid(Joi.ref('password')).required(),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
await accountService.resetPassword(req.body);
|
|
|
|
res.json({ message: 'Password reset successful, you can now login' });
|
|
|
|
}),
|
|
|
|
);
|
2020-10-20 19:25:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
module.exports = router;
|