89 lines
2.7 KiB
JavaScript
89 lines
2.7 KiB
JavaScript
|
const express = require('express');
|
|||
|
const router = express.Router();
|
|||
|
const Joi = require('joi');
|
|||
|
const validateRequest = require('../_middleware/validate-request');
|
|||
|
const accountService = require('./account.service');
|
|||
|
|
|||
|
// routes
|
|||
|
router.post('/register', registerSchema, register);
|
|||
|
router.post('/verify-email', verifyEmailSchema, verifyEmail);
|
|||
|
router.post('/forgot-password', forgotPasswordSchema, forgotPassword);
|
|||
|
router.post('/validate-reset-token', validateResetTokenSchema, validateResetToken);
|
|||
|
router.post('/reset-password', resetPasswordSchema, resetPassword);
|
|||
|
|
|||
|
module.exports = router;
|
|||
|
|
|||
|
function registerSchema(req, res, next) {
|
|||
|
const schema = 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()
|
|||
|
});
|
|||
|
|
|||
|
validateRequest(req, next, schema);
|
|||
|
}
|
|||
|
|
|||
|
function register(req, res, next) {
|
|||
|
accountService.register(req.body, req.get('origin'))
|
|||
|
.then(() => res.json({ message: 'Registration successful, please check your email for verification instructions' }))
|
|||
|
.catch((err) => {
|
|||
|
console.log(err);
|
|||
|
next(err)
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
function verifyEmailSchema(req, res, next) {
|
|||
|
const schema = Joi.object({
|
|||
|
token: Joi.string().required()
|
|||
|
});
|
|||
|
validateRequest(req, next, schema);
|
|||
|
}
|
|||
|
|
|||
|
function verifyEmail(req, res, next) {
|
|||
|
accountService.verifyEmail(req.body)
|
|||
|
.then(() => res.json({ message: 'Verification successful, you can now login' }))
|
|||
|
.catch(next);
|
|||
|
}
|
|||
|
|
|||
|
function forgotPasswordSchema(req, res, next) {
|
|||
|
const schema = Joi.object({
|
|||
|
email: Joi.string().email().required()
|
|||
|
});
|
|||
|
validateRequest(req, next, schema);
|
|||
|
}
|
|||
|
|
|||
|
function forgotPassword(req, res, next) {
|
|||
|
accountService.forgotPassword(req.body, req.get('origin'))
|
|||
|
.then(() => res.json({ message: 'Please check your email for password reset instructions' }))
|
|||
|
.catch(next);
|
|||
|
}
|
|||
|
|
|||
|
function validateResetTokenSchema(req, res, next) {
|
|||
|
const schema = Joi.object({
|
|||
|
token: Joi.string().required()
|
|||
|
});
|
|||
|
validateRequest(req, next, schema);
|
|||
|
}
|
|||
|
|
|||
|
function validateResetToken(req, res, next) {
|
|||
|
accountService.validateResetToken(req.body)
|
|||
|
.then(() => res.json({ message: 'Token is valid' }))
|
|||
|
.catch(next);
|
|||
|
}
|
|||
|
|
|||
|
function resetPasswordSchema(req, res, next) {
|
|||
|
const schema = Joi.object({
|
|||
|
token: Joi.string().required(),
|
|||
|
password: Joi.string().min(6).required(),
|
|||
|
confirmPassword: Joi.string().valid(Joi.ref('password')).required()
|
|||
|
});
|
|||
|
validateRequest(req, next, schema);
|
|||
|
}
|
|||
|
|
|||
|
function resetPassword(req, res, next) {
|
|||
|
accountService.resetPassword(req.body)
|
|||
|
.then(() => res.json({ message: 'Password reset successful, you can now login' }))
|
|||
|
.catch(next);
|
|||
|
}
|