Skip to content

Commit

Permalink
Merge pull request #65 from rtCamp/feature/recaptcha-google-main
Browse files Browse the repository at this point in the history
reCaptcha demo [main]
  • Loading branch information
gagan0123 committed Apr 18, 2024
2 parents df3d90b + ff718ce commit 1361a41
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.example
Expand Up @@ -8,3 +8,4 @@ domain-c-background=bg-red-100
port=8080 # port to listen on
google-client-id= # google client id
facebook-app-id= # facebook app id
recaptcha-site-key= # recaptcha site key
2 changes: 2 additions & 0 deletions app.js
Expand Up @@ -35,6 +35,7 @@ app.use((req, res, next) => {
res.locals.domainC = process.env['domain-c'];
res.locals.googleClientId = process.env['google-client-id'];
res.locals.facebookAppId = process.env['facebook-app-id'];
res.locals.recaptchaSiteKey = process.env['recaptcha-site-key'];
res.locals.port = process.env.port;
res.locals.isPortPresent = req.get('host').includes(':');
res.locals.currentDomain = req.get( 'host' );
Expand Down Expand Up @@ -75,6 +76,7 @@ const scenarios = [
'facebook-comments',
'disqus-comments',
'spotify-embed',
'google-recaptcha',
];
scenarios.forEach(scenario => {
const scenarioRoutes = require(`./src/scenarios/${scenario}/routes`);
Expand Down
4 changes: 2 additions & 2 deletions src/common/index.ejs
Expand Up @@ -21,11 +21,11 @@
<%= renderCard('Facebook Like', '👍', '/facebook-like') %>
<%= renderCard('Facebook Comments', '💬', '/facebook-comments') %>
<%= renderCard('Disqus Comments', '✉️', '/disqus-comments') %>
<%= renderCard('reCAPTCHA', '🤖', '/google-recaptcha') %>
<%= renderCard('Spotify embed', '🔊', '/spotify-embed') %>
<%= renderCard('CHIPS', '🍪', '/chips') %>
<%= renderCard('Storage Access API', '🗃️', '/storage-access-api') %>

</div>
</div>

<%- include(commonPath + '/footer.ejs') %>
<%- include(commonPath + '/footer.ejs') %>
54 changes: 54 additions & 0 deletions src/scenarios/google-recaptcha/index.ejs
@@ -0,0 +1,54 @@
<%- include(commonPath + '/header.ejs') %>

<%- include(commonPath + '/internal-page/header.ejs', {containerType: 'sm'}) %>
<div id="status-message" class="text-center font-bold text-lg my-4"></div>
<form action="?" method="POST" id="captcha-form" class="flex justify-between" >
<div id="html_element"></div>
<input
type="submit"
value="Send"
disabled
class="px-4 py-2 bg-gray-100 text-white rounded transition duration-300 cursor-not-allowed" />
</form>
<%- include(commonPath + '/internal-page/footer.ejs') %>
<script type="text/javascript">
const statusMessage = document.getElementById('status-message');
const captchaForm = document.getElementById('captcha-form');
const sendButton = captchaForm.querySelector('input[type="submit"]')
const verifyCallback = (response) => {
console.log(response);
if (response) {
statusMessage.textContent = `captcha verified`;
sendButton.removeAttribute('disabled');
sendButton.classList.remove('cursor-not-allowed');
sendButton.classList.remove('bg-gray-100');
sendButton.classList.add('bg-blue-500');
}
};
const errorCallback = (error) => {
console.log('error');
if (error) {
statusMessage.textContent = `captcha error`;
}
};
captchaForm.onsubmit = (event) => {
event.preventDefault();
const token = grecaptcha.getResponse();
statusMessage.textContent = `form submitted, captcha token ${token}`;
}
window.onloadCallback = function() {
grecaptcha.render('html_element', {
'sitekey' : '<%= recaptchaSiteKey %>',
'callback' : verifyCallback,
'error-callback' : errorCallback
});
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer>
</script>
<%- include(commonPath + '/footer.ejs') %>
12 changes: 12 additions & 0 deletions src/scenarios/google-recaptcha/routes.js
@@ -0,0 +1,12 @@
const express = require('express');
const path = require('path');
const router = express.Router();

router.get('/', (req, res) => {
// Send the default page
res.render(path.join(__dirname,'index'), {
title: 'Google reCAPTCHA'
});
});

module.exports = router;

0 comments on commit 1361a41

Please sign in to comment.