From b3975192b2162584899b9b7dc35c8bfb45ea53ad Mon Sep 17 00:00:00 2001 From: H9660 Date: Fri, 21 Jun 2024 23:29:14 +0530 Subject: [PATCH] Registration page connected --- backend/package-lock.json | 4 +- .../createAuthMiddleware/authUser.js | 2 +- .../createAuthMiddleware/index.js | 6 ++ .../createAuthMiddleware/register.js | 84 +++++++++++++++++++ backend/src/routes/coreRoutes/coreAuth.js | 2 +- frontend/src/pages/Login.jsx | 10 +++ frontend/src/pages/Register.jsx | 60 +++++++++++++ frontend/src/router/AuthRouter.jsx | 2 + frontend/src/router/routes.jsx | 6 +- 9 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 backend/src/controllers/middlewaresControllers/createAuthMiddleware/register.js create mode 100644 frontend/src/pages/Register.jsx diff --git a/backend/package-lock.json b/backend/package-lock.json index 268204e70..3635c8039 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -1,12 +1,12 @@ { "name": "idurar-erp-crm", - "version": "4.0.0-beta.3", + "version": "4.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "idurar-erp-crm", - "version": "4.0.0-beta.3", + "version": "4.0.0", "license": "Fair-code License", "dependencies": { "@aws-sdk/client-s3": "^3.509.0", diff --git a/backend/src/controllers/middlewaresControllers/createAuthMiddleware/authUser.js b/backend/src/controllers/middlewaresControllers/createAuthMiddleware/authUser.js index e9cbe7359..e71fd4a30 100644 --- a/backend/src/controllers/middlewaresControllers/createAuthMiddleware/authUser.js +++ b/backend/src/controllers/middlewaresControllers/createAuthMiddleware/authUser.js @@ -2,7 +2,7 @@ const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const authUser = async (req, res, { user, databasePassword, password, UserPasswordModel }) => { - const isMatch = await bcrypt.compare(databasePassword.salt + password, databasePassword.password); + const isMatch = await bcrypt.compare(password, databasePassword.password); if (!isMatch) return res.status(403).json({ diff --git a/backend/src/controllers/middlewaresControllers/createAuthMiddleware/index.js b/backend/src/controllers/middlewaresControllers/createAuthMiddleware/index.js index bf45d7b4a..e35bc1e31 100644 --- a/backend/src/controllers/middlewaresControllers/createAuthMiddleware/index.js +++ b/backend/src/controllers/middlewaresControllers/createAuthMiddleware/index.js @@ -1,6 +1,7 @@ const isValidAuthToken = require('./isValidAuthToken'); const login = require('./login'); const logout = require('./logout'); +const register = require('./register'); const forgetPassword = require('./forgetPassword'); const resetPassword = require('./resetPassword'); @@ -17,6 +18,11 @@ const createAuthMiddleware = (userModel) => { userModel, }); + authMethods.register = (req, res) => + register(req, res, { + userModel, + }); + authMethods.forgetPassword = (req, res) => forgetPassword(req, res, { userModel, diff --git a/backend/src/controllers/middlewaresControllers/createAuthMiddleware/register.js b/backend/src/controllers/middlewaresControllers/createAuthMiddleware/register.js new file mode 100644 index 000000000..a20a5f33f --- /dev/null +++ b/backend/src/controllers/middlewaresControllers/createAuthMiddleware/register.js @@ -0,0 +1,84 @@ +const bcrypt = require('bcryptjs'); +const jwt = require('jsonwebtoken'); +const Joi = require('joi'); + +const mongoose = require('mongoose'); + +const checkAndCorrectURL = require('./checkAndCorrectURL'); +const sendMail = require('./sendMail'); + +const { loadSettings } = require('@/middlewares/settings'); +const { useAppSettings } = require('@/settings'); + +const register = async (req, res, { userModel }) => { + const UserPasswordModel = mongoose.model(userModel + 'Password'); + const UserModel = mongoose.model(userModel); + const { name, email, password, country } = req.body; + + // validate + const objectSchema = Joi.object({ + email: Joi.string() + .email({ tlds: { allow: true } }) + .required(), + name: Joi.string().required(), + country: Joi.string().required(), + password: Joi.string().required(), + }); + + const { error, value } = objectSchema.validate({ name, email, password, country }); + if (error) { + return res.status(409).json({ + success: false, + result: null, + error: error, + message: 'Invalid/Missing credentials.', + errorMessage: error.message, + }); + } + + const user = await UserModel.findOne({ email: email, removed: false }); + if (user) + return res.status(409).json({ + success: false, + result: null, + message: 'An account with this email already exists.', + }); + + // authUser if your has correct password + const salt = await bcrypt.genSalt(10); + console.log(salt) + const hashedPassword = await bcrypt.hash(password, salt); + const newUser = { + name: name, + email: email, + country: country, + enabled: true, + }; + + const createdUser = await UserModel.create(newUser); + const newUserPassword = { + removed: false, + user: createdUser, + password: hashedPassword, + salt: salt, + emailVerified: false, + authType: "email", + loggedSessions: [] + } + const databasePassword = await UserPasswordModel.create(newUserPassword); + if (!createdUser || !databasePassword) { + return res.status(500).json({ + success: false, + result: null, + message: 'Error creating your account.', + }); + } else { + const success = { + success: true + } + const newUser = {...createdUser, ...success} + return res.status(200).json(newUser); + } +}; + +module.exports = register; diff --git a/backend/src/routes/coreRoutes/coreAuth.js b/backend/src/routes/coreRoutes/coreAuth.js index 2bda27d4d..5d7eacd43 100644 --- a/backend/src/routes/coreRoutes/coreAuth.js +++ b/backend/src/routes/coreRoutes/coreAuth.js @@ -6,7 +6,7 @@ const { catchErrors } = require('@/handlers/errorHandlers'); const adminAuth = require('@/controllers/coreControllers/adminAuth'); router.route('/login').post(catchErrors(adminAuth.login)); - +router.route('/register').post(catchErrors(adminAuth.register)); router.route('/forgetpassword').post(catchErrors(adminAuth.forgetPassword)); router.route('/resetpassword').post(catchErrors(adminAuth.resetPassword)); diff --git a/frontend/src/pages/Login.jsx b/frontend/src/pages/Login.jsx index c1f8a1f5d..1db894fe7 100644 --- a/frontend/src/pages/Login.jsx +++ b/frontend/src/pages/Login.jsx @@ -51,6 +51,16 @@ const LoginPage = () => { > {translate('Log in')} + diff --git a/frontend/src/pages/Register.jsx b/frontend/src/pages/Register.jsx new file mode 100644 index 000000000..846830c2d --- /dev/null +++ b/frontend/src/pages/Register.jsx @@ -0,0 +1,60 @@ +import { useEffect } from 'react'; + +import { useDispatch, useSelector } from 'react-redux'; +import { useNavigate } from 'react-router-dom'; + +import useLanguage from '@/locale/useLanguage'; + +import { Form, Button } from 'antd'; + +import { selectAuth } from '@/redux/auth/selectors'; +import RegisterForm from '@/forms/RegisterForm'; +import Loading from '@/components/Loading'; +import AuthModule from '@/modules/AuthModule'; +import { register } from '@/redux/auth/actions'; +const RegisterPage = () => { + const translate = useLanguage(); + const { isLoading, isSuccess } = useSelector(selectAuth); + const navigate = useNavigate(); + // const size = useSize(); + + const dispatch = useDispatch(); + const onFinish = (values) => { + console.log("Finsihed") + dispatch(register({ registerData: values })); + navigate('/') + }; + + const FormContainer = () => { + return ( + +
+ + + + + +
+ ); + }; + + return } AUTH_TITLE="Sign Up" />; +}; + +export default RegisterPage; diff --git a/frontend/src/router/AuthRouter.jsx b/frontend/src/router/AuthRouter.jsx index 2416dd984..c63b2b3d6 100644 --- a/frontend/src/router/AuthRouter.jsx +++ b/frontend/src/router/AuthRouter.jsx @@ -1,6 +1,7 @@ import { Routes, Route, Navigate } from 'react-router-dom'; import Login from '@/pages/Login'; +import Register from '@/pages/Register'; import NotFound from '@/pages/NotFound'; import ForgetPassword from '@/pages/ForgetPassword'; @@ -15,6 +16,7 @@ export default function AuthRouter() { } path="/" /> } path="/login" /> + } path="/register"/> } path="/logout" /> } path="/forgetpassword" /> } path="/resetpassword/:userId/:resetToken" /> diff --git a/frontend/src/router/routes.jsx b/frontend/src/router/routes.jsx index 80fd1dd0e..1911b3d65 100644 --- a/frontend/src/router/routes.jsx +++ b/frontend/src/router/routes.jsx @@ -4,7 +4,7 @@ import { Navigate } from 'react-router-dom'; const Logout = lazy(() => import('@/pages/Logout.jsx')); const NotFound = lazy(() => import('@/pages/NotFound.jsx')); - +const Register = lazy(()=> import('@/pages/Register.jsx')); const Dashboard = lazy(() => import('@/pages/Dashboard')); const Customer = lazy(() => import('@/pages/Customer')); const Invoice = lazy(() => import('@/pages/Invoice')); @@ -61,6 +61,10 @@ let routes = { path: '/logout', element: , }, + { + path: '/register', + element: + }, { path: '/about', element: ,