Skip to content

Commit

Permalink
Read recaptcha sitekey from environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Feb 24, 2021
1 parent 520c1c8 commit fd5ee33
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 50 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ HOST=http://localhost:3000
SESSION_SECRET_KEY=F2b}hw'9&M#4rq>#VsN_k{GMu5uecT
TWITCH_CLIENT_ID=12345
TWITCH_LIVESTREAM_URL=https://api.twitch.tv/kraken/streams/?game=Supreme%20Commander:%20Forged%20Alliance
RECAPTCHA_SITE_KEY=6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI

TOKEN_LIFESPAN = 43200 # an api token is valid for 12h
LEAGUES_UPDATE_INTERVAL = 3600 # one hour
Expand Down
2 changes: 1 addition & 1 deletion routes/views/accounts/get/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ exports = module.exports = function (req, res) {
var flash = null;

// Render the view
res.render('account/register', {flash: flash});
res.render('account/register', {flash: flash, recaptchaSiteKey: process.env.RECAPTCHA_SITE_KEY});

};
91 changes: 43 additions & 48 deletions routes/views/accounts/post/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ exports = module.exports = function (req, res) {
let locals = res.locals;

locals.formData = req.body || {};

// validate the input
check('username', 'Username is required').notEmpty();
check('username', 'Username must be three or more characters').isLength({min: 3});
check('email', 'Email is required').notEmpty();
check('email', 'Email does not appear to be valid').isEmail();

// check the validation object for errors
let errors = validationResult(req);

Expand All @@ -41,54 +40,50 @@ exports = module.exports = function (req, res) {
// pull the form variables off the request body
let username = req.body.username;
let email = req.body.email;
let password = req.body.password
let recaptchaResponse = req.body["g-recaptcha-response"]

let overallRes = res;

apiAuth.credentials.getToken()
.then(function (token) {
//Run post to register endpoint
let req = token.sign({
url: process.env.API_URL + '/users/register',
form: {username: username, email: email, password: password}
});
request.post(req, function (err, res, body) {
let resp;
let errorMessages = [];

if (res.statusCode !== 200) {
try {
resp = JSON.parse(body);
} catch (e) {
errorMessages.push({msg: 'Invalid registration sign up. Please try again later.'});
flash.class = 'alert-danger';
flash.messages = errorMessages;
flash.type = 'Error!';

return overallRes.render('account/register', {flash: flash});
}

// Failed registering user
for (let i = 0; i < resp.errors.length; i++) {
let error = resp.errors[i];

errorMessages.push({msg: error.detail});
}

flash.class = 'alert-danger';
flash.messages = errorMessages;
flash.type = 'Error!';

return overallRes.render('account/register', {flash: flash});
}

// Successfully registered user
flash.class = 'alert-success';
flash.messages = [{msg: 'Please check your email to verify your registration. Then you will be ready to log in!'}];
flash.type = 'Success!';

overallRes.render('account/register', {flash: flash});
});
});
//Run post to register endpoint
request.post({
url: process.env.API_URL + '/users/register',
form: {username: username, email: email, recaptchaResponse: recaptchaResponse}
}, function (err, res, body) {
let resp;
let errorMessages = [];

if (res.statusCode !== 200) {
try {
resp = JSON.parse(body);
} catch (e) {
errorMessages.push({msg: 'Invalid registration sign up. Please try again later.'});
flash.class = 'alert-danger';
flash.messages = errorMessages;
flash.type = 'Error!';

return overallRes.render('account/register', {flash: flash});
}

// Failed registering user
for (let i = 0; i < resp.errors.length; i++) {
let error = resp.errors[i];

errorMessages.push({msg: error.detail});
}

flash.class = 'alert-danger';
flash.messages = errorMessages;
flash.type = 'Error!';

return overallRes.render('account/register', {flash: flash});
}

// Successfully registered user
flash.class = 'alert-success';
flash.messages = [{msg: 'Please check your email to verify your registration. Then you will be ready to log in!'}];
flash.type = 'Success!';

overallRes.render('account/register', {flash: flash});
});
}
};
2 changes: 1 addition & 1 deletion templates/views/account/register.pug
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ block content
+tosagree
.form-group.has-feedback
label
.g-recaptcha(data-sitekey='6LctpD0UAAAAAAb4uwVXytn3faLRnUtEch8RimfC')
.g-recaptcha(data-sitekey=recaptchaSiteKey)
p We will send you an email with a link. The link will lead you to a page where you can set your password and activate your account.
p If you don't receive an email please check your spam folder!
.form-actions
Expand Down

0 comments on commit fd5ee33

Please sign in to comment.