-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Registration page connected #1140
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do we use email sending logic? |
||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the comment above, we could reuse existing hashing logic from here. Maybe @salahlalami can confirm |
||
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<Loading isLoading={isLoading}> | ||
<Form | ||
layout="vertical" | ||
name="normal_login" | ||
className="login-form" | ||
initialValues={{ | ||
remember: true, | ||
}} | ||
onFinish={onFinish} | ||
> | ||
<RegisterForm /> | ||
<Form.Item> | ||
<Button | ||
type="primary" | ||
htmlType="submit" | ||
className="login-form-button" | ||
loading={isLoading} | ||
size="large" | ||
> | ||
{translate('Register')} | ||
</Button> | ||
</Form.Item> | ||
</Form> | ||
</Loading> | ||
); | ||
}; | ||
|
||
return <AuthModule authContent={<FormContainer />} AUTH_TITLE="Sign Up" />; | ||
}; | ||
|
||
export default RegisterPage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we should stick to the existing hashing logic here otherwise admin login will fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I will adjust this. Thanks for bringing this into view.