Skip to content

Commit

Permalink
Merge pull request #440 from filipedeschamps/UserProvider
Browse files Browse the repository at this point in the history
feat(use-user): use localStorage cached user
  • Loading branch information
filipedeschamps authored Jun 14, 2022
2 parents c24a08a + b924f20 commit 35db16c
Show file tree
Hide file tree
Showing 15 changed files with 538 additions and 477 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"extends": ["next", "next/core-web-vitals", "prettier", "plugin:primer-react/recommended", "plugin:jsx-a11y/recommended"],
"extends": [
"next",
"next/core-web-vitals",
"prettier",
"plugin:primer-react/recommended",
"plugin:jsx-a11y/recommended"
],
"plugins": ["jsx-a11y"],
"rules": {
"react/no-unescaped-entities": "off",
Expand Down
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.next
pages/pocs
.husky
.husky
public/museu
18 changes: 17 additions & 1 deletion infra/scripts/seed-database.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('node:fs');
const { join, resolve } = require('path');

const { Client } = require('pg');
const client = new Client({
connectionString: 'postgres://local_user:local_password@localhost:54320/tabnews',
Expand All @@ -13,9 +16,10 @@ async function seedDatabase() {

await client.connect();
await seedDevelopmentUsers();
await createFirewallFunctions();
await client.end();

console.log('> Database seeded!');
console.log('\n> Database seeded!');
}

async function seedDevelopmentUsers() {
Expand Down Expand Up @@ -63,3 +67,15 @@ async function seedDevelopmentUsers() {
}
}
}

async function createFirewallFunctions() {
console.log('\n> Creating Firewall functions...');
const proceduresPath = join(resolve('.'), 'infra', 'stored-procedures');
const procedures = fs.readdirSync(proceduresPath);

for (const procedureFile of procedures) {
const procedureQuery = fs.readFileSync(`${proceduresPath}/${procedureFile}`, 'utf8');
await client.query(procedureQuery);
}
console.log('> Firewall functions created!');
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BEGIN
WHERE
originator_ip = clientIp
AND type = 'create:content:text_child'
AND created_at > NOW() - INTERVAL '1 minute'
AND created_at > NOW() - INTERVAL '5 seconds'
);

IF contents_count >= 2 THEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BEGIN
WHERE
originator_ip = clientIp
AND type = 'create:content:text_root'
AND created_at > NOW() - INTERVAL '1 minute'
AND created_at > NOW() - INTERVAL '5 seconds'
);

IF contents_count >= 2 THEN
Expand Down
2 changes: 1 addition & 1 deletion infra/stored-procedures/firewall-create-user.pgsql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BEGIN
WHERE
originator_ip = clientIp
AND type = 'create:user'
AND created_at > NOW() - INTERVAL '1 minute'
AND created_at > NOW() - INTERVAL '5 seconds'
);

IF users_count >= 2 THEN
Expand Down
4 changes: 4 additions & 0 deletions models/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ async function injectRequestMetadata(request, response, next) {
function extractAnonymousIpFromRequest(request) {
let ip = request.headers['x-real-ip'] || request.connection.remoteAddress;

if (ip === '::1') {
ip = '127.0.0.1';
}

if (ip.substr(0, 7) == '::ffff:') {
ip = ip.substr(7);
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"test:watch:services": "kill-port 3000 && npm run services:up && concurrently -s -k -n next,jest 'npm run next' 'jest --runInBand --watchAll' && npm run services:stop",
"lint": "npm run lint:next && npm run lint:prettier",
"lint:next": "next lint --max-warnings=0 --dir .",
"lint:prettier": "prettier --check **/*.{json,js,jsx,ts,tsx}",
"lint:fix": "eslint --fix && prettier --write **/*.{json,js,jsx,ts,tsx}",
"lint:prettier": "prettier --check .",
"lint:fix": "eslint --fix && prettier --write .",
"commit": "cz",
"migration:create": "node-pg-migrate create",
"migration:run": "node infra/scripts/wait-for-db-connection-ready.js && node-pg-migrate up --envPath ./.env -m infra/migrations/ 2>migrations.log",
Expand Down
27 changes: 15 additions & 12 deletions pages/_app.public.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ThemeProvider, BaseStyles, SSRProvider } from '@primer/react';
import { SWRConfig } from 'swr';
import { UserProvider } from 'pages/interface/hooks/useUser/index.js';

async function SWRFetcher(resource, init) {
const response = await fetch(resource, init);
Expand All @@ -10,18 +11,20 @@ async function SWRFetcher(resource, init) {

function MyApp({ Component, pageProps }) {
return (
<SWRConfig
value={{
fetcher: SWRFetcher,
}}>
<SSRProvider>
<ThemeProvider preventSSRMismatch colorMode="day">
<BaseStyles>
<Component {...pageProps} />
</BaseStyles>
</ThemeProvider>
</SSRProvider>
</SWRConfig>
<UserProvider>
<SWRConfig
value={{
fetcher: SWRFetcher,
}}>
<SSRProvider>
<ThemeProvider preventSSRMismatch colorMode="day">
<BaseStyles>
<Component {...pageProps} />
</BaseStyles>
</ThemeProvider>
</SSRProvider>
</SWRConfig>
</UserProvider>
);
}

Expand Down
1 change: 1 addition & 0 deletions pages/cadastro/confirmar/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function ConfirmSignup() {

useEffect(() => {
const userEmail = localStorage.getItem('registrationEmail');
localStorage.removeItem('registrationEmail');
setEmail(userEmail);
}, []);

Expand Down
Loading

1 comment on commit 35db16c

@vercel
Copy link

@vercel vercel bot commented on 35db16c Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tabnews – ./

tabnews-git-main-tabnews.vercel.app
tabnews-tabnews.vercel.app
www.tabnews.com.br
tabnews.com.br

Please sign in to comment.