Skip to content

Commit

Permalink
Merge pull request #51 from lapig-ufg/feature/reCaptcha
Browse files Browse the repository at this point in the history
ReCaptcha
  • Loading branch information
James-jamames authored Dec 11, 2023
2 parents 7200594 + ff9d87c commit a4d97dd
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 47 deletions.
32 changes: 32 additions & 0 deletions src/client/package-lock.json

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

3 changes: 2 additions & 1 deletion src/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"jspdf-autotable": "^3.5.23",
"lodash-es": "^4.17.21",
"moment": "^2.29.1",
"ng-recaptcha": "^8.0.1",
"ngx-drag-scroll": "^12.0.0-beta.1",
"ol": "6.14.1",
"ol-ext": "3.2.13",
Expand All @@ -66,4 +67,4 @@
"karma-jasmine-html-reporter": "~1.7.0",
"typescript": "~4.3.2"
}
}
}
2 changes: 1 addition & 1 deletion src/client/src/app/@core/interfaces/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export interface Contact {
subject: string;
institution: string;
message: string;
status: string;
token: string;
}
11 changes: 9 additions & 2 deletions src/client/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { environment } from '../environments/environment';
import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';
import { NoCacheHeaders } from "./@core/interceptors/no-cache-headers.interceptor";

import { RECAPTCHA_V3_SITE_KEY, RecaptchaV3Module } from 'ng-recaptcha';

/**
* The http loader factory : Loads the files from define path.
* @param {HttpClient} http
Expand All @@ -31,6 +33,7 @@ export function HttpLoaderFactory(http: HttpClient) {
AppComponent
],
imports: [
RecaptchaV3Module,
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule,
Expand All @@ -50,11 +53,15 @@ export function HttpLoaderFactory(http: HttpClient) {
}
}),
],
providers: [ {
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: NoCacheHeaders,
multi: true
},],
},
{
provide: RECAPTCHA_V3_SITE_KEY,
useValue: environment.recaptcha.siteKey,
}],
bootstrap: [AppComponent],
})

Expand Down
61 changes: 33 additions & 28 deletions src/client/src/app/hotsite/pages/contato/contato.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { error } from 'console';
import { MessageService } from 'primeng/api';
//import {InputTextModule} from 'primeng/inputtext';
import {InputTextareaModule} from 'primeng/inputtextarea';
import { LocalizationService } from 'src/app/@core/internationalization/localization.service';
import { ContatoService } from './contato.service';
import { ReCaptchaV3Service } from 'ng-recaptcha';

@Component({
selector: 'app-contato',
Expand All @@ -19,41 +17,48 @@ export class ContatoComponent implements OnInit {
constructor(
private contatoService: ContatoService,
protected messageService: MessageService,
public localizationService: LocalizationService) {
public localizationService: LocalizationService,
private recaptchaV3Service: ReCaptchaV3Service) {
}

ngOnInit() {
}

onSubmit(contactForm: NgForm) {
if (contactForm.valid) {
this.erroForm = false;

const contact = contactForm.value;
contact.status = "RECEIVED";
this.contatoService.saveContact(contactForm.value).subscribe((result) => {
if(result.message === "sucess") {
contactForm.reset();
this.recaptchaV3Service.execute('importantAction')
.subscribe((token: string) => {
if (contactForm.valid) {
this.erroForm = false;

const contact = contactForm.value;
contact.token = token;
console.log(contact);
this.contatoService.saveContact(contact).subscribe((result) => {
if(result.message === "sucess") {
contactForm.reset();
this.messageService.add({
key: "contact-message",
life: 2000,
severity: 'success',
summary: this.localizationService.translate('hotsite.contact.submit-message.success'),
detail: this.localizationService.translate('hotsite.contact.submit-message.success')
})
}
}, (error)=>{
this.messageService.add({
key: "contact-message",
life: 2000,
severity: 'success',
summary: this.localizationService.translate('hotsite.contact.submit-message.success'),
detail: this.localizationService.translate('hotsite.contact.submit-message.success')
severity: 'error',
summary: this.localizationService.translate('hotsite.contact.submit-message.fail'),
detail: this.localizationService.translate('hotsite.contact.submit-message.fail')
})
}
}, (error)=>{
this.messageService.add({
key: "contact-message",
life: 2000,
severity: 'error',
summary: this.localizationService.translate('hotsite.contact.submit-message.fail'),
detail: this.localizationService.translate('hotsite.contact.submit-message.fail')
})
});
} else {
this.erroForm = true;
}
});
} else {
this.erroForm = true;
}
});


}

getErroForm(): boolean {
Expand Down
5 changes: 4 additions & 1 deletion src/client/src/environments/environment.homolog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ export const environment = {
LAPIG_DOWNLOAD_API: 'https://download.lapig.iesa.ufg.br',
S3: "https://s3.lapig.iesa.ufg.br/storage/",
MAX_AREA: 9500,
COMMIT_ID:commitId
COMMIT_ID:commitId,
recaptcha: {
siteKey: '6Lf_jygpAAAAAO-8ArU28R6EszgO2WhCArk06nPm',
},
};
5 changes: 4 additions & 1 deletion src/client/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ export const environment = {
LAPIG_DOWNLOAD_API: 'https://download.lapig.iesa.ufg.br',
S3: "https://s3.lapig.iesa.ufg.br/storage/",
MAX_AREA: 9500,
COMMIT_ID:commitId
COMMIT_ID:commitId,
recaptcha: {
siteKey: '6Lf_jygpAAAAAO-8ArU28R6EszgO2WhCArk06nPm',
},
};
5 changes: 4 additions & 1 deletion src/client/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const environment = {
LAPIG_CONTENT_HUB: 'https://content-hub.lapig.iesa.ufg.br',
S3: "https://s3.lapig.iesa.ufg.br/storage/",
MAX_AREA: 9500,
COMMIT_ID:commitId
COMMIT_ID:commitId,
recaptcha: {
siteKey: '6Lf_jygpAAAAAO-8ArU28R6EszgO2WhCArk06nPm',
},
};

/*
Expand Down
5 changes: 0 additions & 5 deletions src/server/controllers/contact.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
module.exports = function (app) {
var Controller = {}
var Internal = {}

Controller.create = function (request, response) {
// var rows = request.queryResult['create'];

response.status(201).send({ message: "sucess" });
response.end();

}

return Controller;

}
8 changes: 3 additions & 5 deletions src/server/database/queries/contact.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
module.exports = function(app) {

var Internal = {}
var Query = {};

Query.defaultParams = {}

Query.create = function(params) {

const { name, email, subject, institution, message, status } = params['contact'];
const { name, email, subject, institution, message } = params['contact'];

return [{
source: 'lapig',
id: 'create',
sql: `INSERT INTO public.contato_atlas ("name", email, subject, message, institution, status) VALUES('${name}', '${email}', '${subject}', '${message}', '${institution}', '${status}');`,
sql: `INSERT INTO public.contato_atlas ("name", email, subject, message, institution, status) VALUES('${name}', '${email}', '${subject}', '${message}', '${institution}', 'RECEIVED');`,
mantain: true
}
]
}

return Query;

z
}
1 change: 0 additions & 1 deletion src/server/middleware/data-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = function(app) {
}

return function(request, response, next) {

var hasController = (request.route.stack.length > 1)
var pathParts = request.path.split('/')
var controller = pathParts[2]
Expand Down
26 changes: 26 additions & 0 deletions src/server/middleware/re-captcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const axios = require('axios')

module.exports = function (app) {
var Internal = {}

var api = 'https://www.google.com/recaptcha/api/siteverify';

return function (request, response, next) {
axios({
method: 'post',
url: api,
headers: { "Content-Type": "application/x-www-form-urlencoded" },
data: `secret=${process.env.RECAPTCHA_KEY}&response=${request.body.contact.token}`,
}).then(recaptcha => {
if(recaptcha.data.success) {
next();
} else {
response.status(401)
.send({ message: "You are a robot" })
}
}).catch(error => {
response.status(501)
.send({ message: "Sever error" })
});
}
}
Loading

0 comments on commit a4d97dd

Please sign in to comment.