Skip to content
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

Recaptcha added for robot verification #1139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#DATABASE = "mongodb://localhost:27017"
#RESEND_API = "your resend_api"
#OPENAI_API_KEY = "your open_ai api key"
DATABASE = "mongodb://localhost:27017"
RESEND_API = "your resend_api"
OPENAI_API_KEY = "your open_ai api key"
JWT_SECRET= "your_private_jwt_secret_key"
NODE_ENV = "production"
OPENSSL_CONF='/dev/null'
PUBLIC_SERVER_FILE="http://localhost:8888/"
# RECAPTCHA_SECRET_KEY = RECAPTCHA_SECRET_KEY
39 changes: 37 additions & 2 deletions backend/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 backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.509.0",
"axios": "^1.7.2",
"bcryptjs": "^2.4.3",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ const { loadSettings } = require('@/middlewares/settings');
const { useAppSettings } = require('@/settings');

const authUser = require('./authUser');
const reCaptcha = require('./reCaptcha')

const login = async (req, res, { userModel }) => {
const UserPasswordModel = mongoose.model(userModel + 'Password');
const UserModel = mongoose.model(userModel);
const { email, password } = req.body;

const { loginData,recaptchaToken} = req.body;
const {email,password} = loginData;

// validate
const objectSchema = Joi.object({
email: Joi.string()
.email({ tlds: { allow: true } })
.required(),
password: Joi.string().required(),
});
password: Joi.string().required(),
recaptchaToken : Joi.string().required()
});

const { error, value } = objectSchema.validate({ email, password });
const { error, value } = objectSchema.validate({ email, password,recaptchaToken });
if (error) {
return res.status(409).json({
success: false,
Expand Down Expand Up @@ -55,7 +59,8 @@ const login = async (req, res, { userModel }) => {
message: 'Your account is disabled, contact your account adminstrator',
});

// authUser if your has correct password
await reCaptcha(recaptchaToken);

authUser(req, res, { user, databasePassword, password, UserPasswordModel });
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const axios = require('axios')
const reCaptcha = async({response}) =>{
const SECRET_KEY = process.env.RECAPTCHA_SECRET_KEY;
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${SECRET_KEY}&response=${response}`;

try{ const reCaptchaResponse = await axios.post(url,{});
if(reCaptchaResponse.success == false)
return res.status(409).json({
success: false,
result: null,
message: 'Captcha was wrong.Retry',
}); }
catch(error){
console.error('Error verifying reCAPTCHA:', error);
return {
success: false,
result: null,
message: 'Error verifying reCAPTCHA',
};
}
}
module.exports = reCaptcha;
3 changes: 2 additions & 1 deletion frontend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# ----> Remove # comment
VITE_FILE_BASE_URL = 'http://localhost:8888/'
VITE_BACKEND_SERVER="http://your_backend_url_server.com/"
PROD = false
PROD = false
# SITE_KEY = RECAPTCHA_SITE_KEY
1 change: 1 addition & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="icon" type="image/x-icon" href="./src/favicon.ico" />
<title>IDURAR ERP CRM | Open Code Source</title>
<meta name="description" content="IDURAR ERP CRM Open Source with Multi-Currency" />
<script src="https://www.google.com/recaptcha/api.js?render=YOUR_SITE_KEY"></script>
</head>
<body>
<div id="root"></div>
Expand Down
4 changes: 2 additions & 2 deletions frontend/package-lock.json

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

13 changes: 9 additions & 4 deletions frontend/src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ const LoginPage = () => {
const navigate = useNavigate();
// const size = useSize();

const dispatch = useDispatch();
const onFinish = (values) => {
dispatch(login({ loginData: values }));
};
const onFinish = async (values) => {
try {
const token = await grecaptcha.execute(YOUR_SITE_KEY, { action: 'submit' });
console.log(token);
dispatch(login({ loginData: values, recaptchaToken: token }));
} catch (error) {
console.error('Error executing reCAPTCHA:', error);
}
}

useEffect(() => {
if (isSuccess) navigate('/');
Expand Down