Skip to content

Commit

Permalink
Merge pull request #139 from Deepanshu0703/captcha
Browse files Browse the repository at this point in the history
feat(backend): added captcha verification in signup
  • Loading branch information
jain-rishabh-21 authored Aug 9, 2023
2 parents b064f69 + d897702 commit 73b952a
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 14 deletions.
25 changes: 25 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-google-recaptcha": "^3.1.0",
"react-icons": "^4.10.1",
"react-loader-spinner": "^5.3.4",
"react-location": "^3.3.4",
Expand Down
17 changes: 14 additions & 3 deletions client/src/pages/SignUp/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { useNavigate } from 'react-router-dom'
import { FaUserAlt } from 'react-icons/fa'
import { MdEmail, MdPassword } from 'react-icons/md'
import { AiFillEyeInvisible, AiFillEye, AiFillGoogleCircle, AiFillFacebook } from 'react-icons/ai'
import ReCAPTCHA from "react-google-recaptcha"

export default function SignUp() {
const [details, setDetails] = React.useState({ name: '', email: '', password: '', confirmPassword: '' })
const [details, setDetails] = React.useState({ name: '', email: '', password: '', confirmPassword: '', Token: '' })
const navigate = useNavigate()
const handleSignUp = async (e) => {
e.preventDefault()
const { name, email, password } = details
const user = { name, email, password }
const { name, email, password, Token } = details
const user = { name, email, password, Token }
try {
if (password !== details.confirmPassword) {
alert('Passwords do not match')
Expand Down Expand Up @@ -56,6 +57,8 @@ export default function SignUp() {
}
}

const reRef = React.createRef(null);

return (
<>
<div className='rectangleRight'></div>
Expand All @@ -64,6 +67,7 @@ export default function SignUp() {

{/* Login Actual Div */}
<div className='loginDiv'>

<form className='formDiv'>
<h3>Doctor Verse</h3>
{/* username */}
Expand Down Expand Up @@ -155,6 +159,11 @@ export default function SignUp() {
}
</div>
</div>
<ReCAPTCHA
onChange={(token) => setDetails({ ...details, Token: token })}
sitekey="Your client site key"
/>
<br />

{/* Form Buttons */}
<div className='flex justify-around pb-3 items-center gap-6'>
Expand All @@ -176,6 +185,8 @@ export default function SignUp() {
</p>
</div>



</form>
</div>

Expand Down
2 changes: 1 addition & 1 deletion server/api/controller/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { decodePassword, encodePassword } = require('../Authentication/passwordEn
const User = require('../models/doctor');
const { resetToken } = require('../Authentication/jwt');
const { resetEmail } = require('../Authentication/template');
const { transporter } = require('../Authentication/transportmail');


const changePassword = async (req, res) => {
try {
Expand Down
33 changes: 24 additions & 9 deletions server/api/controller/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ const User = require('../models/user');
const { encodePassword, decodePassword } = require('../Authentication/passwordEncodeDecode');
const { userLoginSchema, userRegisterSchema, userUpdateSchema } = require('../validations/user');
const { tokenSign } = require('../Authentication/jwt');
const axios = require('axios');

// SIGNUP-CONTROLLER
const registerUser = async (req, res) => {
try {
const payloadValidate = await userRegisterSchema.validateAsync(req.body);
const { name, email, password } = payloadValidate;
const hashPassword = await encodePassword(password);
const user = new User({
name,
email,
password: hashPassword
const payloadValidate = await userRegisterSchema.validateAsync({
name: req.body.name,
email: req.body.email,
password: req.body.password
});
await user.save();
res.status(201).json({ name, email });
const { name, email, password } = payloadValidate;
//Captcha Verification
const secret = "YOUR SECRET KEY";
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=${req.body.Token}`;
const response = await axios.post(url);
if (response.data.success) {

const hashPassword = await encodePassword(password);
const user = new User({
name,
email,
password: hashPassword
});
await user.save();
res.status(201).json({ name, email });
}
else {
res.status(400).json({ message: 'Invalid Captcha' });
}
} catch (e) {
res.status(400).json({ message: e.message });
}
Expand Down
2 changes: 1 addition & 1 deletion server/api/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const router = express.Router();
const { loginUser, registerUser, updateUser } = require('../controller/user');
const { tokenVerify } = require('../Authentication/jwt');


// SIGNUP-ENDPOINT
router.post('/userSignUp', registerUser);

// LOGIN-ENDPOINT
router.post('/userLogin', loginUser);

Expand Down
72 changes: 72 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"nodemon": "^2.0.15"
},
"dependencies": {
"axios": "^1.4.0",
"bcryptjs": "^2.4.3",
"concurrently": "^7.0.0",
"cors": "^2.8.5",
Expand Down

0 comments on commit 73b952a

Please sign in to comment.