Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Joi = require('joi');
- const HttpStatus = require('http-status-codes');
- const bcrypt = require('bcryptjs');
- const jwt = require('jsonwebtoken');
- const User = require('../models/userModels');
- const Helpers = require('../helpers/helpers');
- const dbConfig = require('../config/secret');
- module.exports = {
- async CreateUser(req, res) {
- const schema = Joi.object().keys({
- username: Joi.string().min(5).max(15).required(),
- email: Joi.string().email().required(),
- password: Joi.string().min(5).max(15).required()
- });
- const {
- error,
- value
- } = Joi.validate(req.body, schema);
- if (error && error.details) {
- return res.status(HttpStatus.BAD_REQUEST).json({
- message: error.details
- })
- }
- const userEmail = await User.findOne({
- email: Helpers.lowerCase(req.body.email)
- });
- if (userEmail) {
- return res.status(HttpStatus.CONFLICT).json({
- message: 'Email already exist'
- });
- }
- const userName = await User.findOne({
- username: Helpers.firstUpper(req.body.username)
- });
- if (userName) {
- return res.status(HttpStatus.CONFLICT).json({
- message: 'Username alreay exists'
- });
- }
- return bcrypt.hash(value.password, 10, (err, hash) => {
- if (err) {
- return res.status(HttpStatus.CONFLICT).json({
- message: 'Error hashing password'
- });
- }
- const body = {
- username: Helpers.firstUpper(value.username),
- email: Helpers.lowerCase(value.email),
- password: hash
- };
- User.create(body).then((user) => {
- const token = jwt.sign({
- data: user
- }, dbConfig.secret, {
- expiresIn: 120
- });
- res.cookie('auth', token);
- res.status(HttpStatus.CREATED).json({
- message: 'User created successfully',
- user,
- token
- });
- }).catch(error => {
- res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
- message: 'Error ocurred ' + error
- });
- });
- })
- }
- };
- const express = require('express');
- const router = express.Router();
- const AuthController = require('../controllers/auth');
- router.post('/register', AuthController.CreateUser);
- module.exports = router;
- const express = require('express');
- const mongoose = require('mongoose');
- const cookieParser = require('cookie-parser');
- const cors = require('cors');
- //const logger = require('morgan');
- const app = express();
- app.use(cors());
- const dbConfig = require('./config/secret');
- app.use((req, res, next) => {
- res.header('Access-Control-Allow-Origin', '*');
- res.header('Access-Control-Allow-Credentials', 'true');
- res.header('Access-Control-Allow-Methods', 'GET', 'POST', 'DELETE', 'PUT', 'OPTIONS');
- res.header(
- 'Access-Control-Allow-Headers',
- 'Origin, X-Requested-Width, Content-Type, Accept, Authorization'
- );
- next();
- });
- app.use(express.json({
- limit: '50mb'
- }));
- app.use(express.urlencoded({
- extended: true,
- limit: '50mb'
- }));
- app.use(cookieParser);
- //app.use(logger('dev'));
- mongoose.Promise = global.Promise;
- mongoose.connect(dbConfig.url, {
- useNewUrlParser: true
- });
- const auth = require('./routes/authRoutes');
- app.use('/api/chatapp', auth); // For example http://localhost:3000/api/chatapp/register;
- app.listen(3000, () => {
- console.log('Running on port 3000');
- });
Add Comment
Please, Sign In to add comment