Proxy front-end from back-end

master
Ian Mancini 4 years ago
parent 43cdcd3307
commit 5fad05ac31

6986
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,5 +1,4 @@
module.exports = { module.exports = {
root: true,
parser: '@typescript-eslint/parser', parser: '@typescript-eslint/parser',
parserOptions: { parserOptions: {
ecmaVersion: 2020, ecmaVersion: 2020,
@ -13,24 +12,13 @@ module.exports = {
version: 'detect', version: 'detect',
}, },
}, },
env: {
browser: true,
amd: true,
node: true,
},
plugins: ['simple-import-sort'],
extends: [ extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended', 'plugin:react/recommended',
'plugin:jsx-a11y/recommended', 'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint', 'prettier/@typescript-eslint',
'plugin:prettier/recommended', 'plugin:prettier/recommended',
], ],
rules: { rules: {
'prettier/prettier': ['error', {}, { usePrettierrc: true }], 'react/prop-types': ['off'],
'react/prop-types': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'simple-import-sort/sort': 'error',
}, },
} }

File diff suppressed because it is too large Load Diff

@ -3,6 +3,7 @@
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@chakra-ui/core": "^1.0.0-rc.7",
"@testing-library/jest-dom": "^5.11.4", "@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0", "@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10", "@testing-library/user-event": "^12.1.10",
@ -10,6 +11,7 @@
"@types/node": "^12.0.0", "@types/node": "^12.0.0",
"@types/react": "^16.9.53", "@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8", "@types/react-dom": "^16.9.8",
"framer-motion": "^2.9.4",
"http-proxy-middleware": "^1.0.6", "http-proxy-middleware": "^1.0.6",
"react": "^17.0.1", "react": "^17.0.1",
"react-dom": "^17.0.1", "react-dom": "^17.0.1",

@ -1,7 +1,19 @@
import { ChakraProvider, Heading, Stack, Link, VStack } from '@chakra-ui/core'
import React from 'react' import React from 'react'
function App(): JSX.Element { function App(): JSX.Element {
return <div>cliente</div> return (
<ChakraProvider resetCSS>
<VStack>
<Heading> museo.red </Heading>
<Stack>
<Link href="/auth/facebook">ingresar con facebook</Link>
<Link href="/auth/twitter">ingresar con facebook</Link>
<Link href="/auth/google">ingresar con google</Link>
</Stack>
</VStack>
</ChakraProvider>
)
} }
export default App export default App

@ -3,7 +3,7 @@ const { createProxyMiddleware } = require('http-proxy-middleware')
module.exports = (app) => { module.exports = (app) => {
app.use( app.use(
['/auth', '/socket.io'], ['/socket.io'],
createProxyMiddleware({ createProxyMiddleware({
target: 'http://localhost:3000', target: 'http://localhost:3000',
changeOrigin: true, changeOrigin: true,

@ -1,6 +1,6 @@
{ {
"extends": ["eslint:recommended", "prettier"], "extends": ["eslint:recommended", "prettier"],
"plugins": ["prettier", "simple-import-sort"], "plugins": ["prettier"],
"rules": {}, "rules": {},
"root": true, "root": true,
"parserOptions": { "parserOptions": {
@ -11,8 +11,7 @@
"es6": true, "es6": true,
"node": true "node": true
}, },
"rules": { "rules": {
"prettier/prettier": ["error", {}, { "usePrettierrc": true }], "prettier/prettier": ["error", {}, { "usePrettierrc": true }]
"simple-import-sort/sort": "error" }
}
} }

@ -2364,6 +2364,11 @@
"is-symbol": "^1.0.2" "is-symbol": "^1.0.2"
} }
}, },
"es6-promise": {
"version": "4.2.8",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.8.tgz",
"integrity": "sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w=="
},
"escalade": { "escalade": {
"version": "3.1.1", "version": "3.1.1",
"resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz",
@ -2730,6 +2735,31 @@
"vary": "~1.1.2" "vary": "~1.1.2"
} }
}, },
"express-http-proxy": {
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/express-http-proxy/-/express-http-proxy-1.6.2.tgz",
"integrity": "sha512-soP7UXySFdLbeeMYL1foBkEoZj6HELq9BDAOCr1sLRpqjPaFruN5o6+bZeC+7U4USWIl4JMKEiIvTeKJ2WQdlQ==",
"requires": {
"debug": "^3.0.1",
"es6-promise": "^4.1.1",
"raw-body": "^2.3.0"
},
"dependencies": {
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "^2.1.1"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"express-session": { "express-session": {
"version": "1.17.1", "version": "1.17.1",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.1.tgz", "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.1.tgz",

@ -7,7 +7,7 @@
"scripts": { "scripts": {
"build": "babel src --out-dir build --copy-files", "build": "babel src --out-dir build --copy-files",
"serve": "node build/index.js", "serve": "node build/index.js",
"start": "nodemon -w src --exec \"yarn build && yarn serve\"", "start": "nodemon -w src --exec \"npm run build && npm run serve\"",
"lint": "eslint src/**/*.js*", "lint": "eslint src/**/*.js*",
"fix": "eslint --fix src/**/*.js*" "fix": "eslint --fix src/**/*.js*"
}, },
@ -38,6 +38,7 @@
"cookie-parser": "^1.4.5", "cookie-parser": "^1.4.5",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-http-proxy": "^1.6.2",
"express-session": "^1.17.1", "express-session": "^1.17.1",
"mongoose": "^5.10.10", "mongoose": "^5.10.10",
"passport": "^0.4.1", "passport": "^0.4.1",

@ -1,8 +1,8 @@
import path from 'path' import path from 'path'
import dotenv from 'dotenv' import dotenv from 'dotenv'
dotenv.config({ path: path.join(__dirname, '../../../.env') }); dotenv.config({ path: path.join(__dirname, '../../../.env') })
import { Strategy } from 'passport-facebook'; import { Strategy } from 'passport-facebook'
import { Router } from 'express' import { Router } from 'express'
import passport from 'passport' import passport from 'passport'
@ -13,19 +13,25 @@ const strategyOptions = {
clientID: process.env.FACEBOOK_CLIENT_ID, clientID: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET, clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
callbackURL: '/auth/facebook/redirect', callbackURL: '/auth/facebook/redirect',
profileFields: ['id', 'displayName', 'picture.type(large)', 'email'] profileFields: ['id', 'displayName', 'picture.type(large)', 'email'],
} }
export function facebookStrategy() { export function facebookStrategy() {
return new Strategy(strategyOptions, genericStrategy('facebook')); return new Strategy(strategyOptions, genericStrategy('facebook'))
} }
export const facebookRouter = Router() export const facebookRouter = Router()
const facebookAuthenticateOptions = { const facebookAuthenticateOptions = {
authType: 'rerequest', scope: ['email'] authType: 'rerequest',
scope: ['email'],
} }
facebookRouter.get('/facebook', passport.authenticate('facebook', facebookAuthenticateOptions)); facebookRouter.get(
facebookRouter.get('/facebook/redirect', '/facebook',
passport.authenticate('facebook', { successRedirect: '/museo', failureRedirect: '/' })); passport.authenticate('facebook', facebookAuthenticateOptions),
)
facebookRouter.get(
'/facebook/redirect',
passport.authenticate('facebook', { successRedirect: '/museo', failureRedirect: '/' }),
)

@ -1,11 +1,12 @@
import 'regenerator-runtime/runtime'; import 'regenerator-runtime/runtime'
import path from 'path' import path from 'path'
import dotenv from 'dotenv' import dotenv from 'dotenv'
dotenv.config({ path: path.join(__dirname, '../.env') }); dotenv.config({ path: path.join(__dirname, '../.env') })
import http from 'http'; import http from 'http'
import express from 'express'; import express from 'express'
import proxy from 'express-http-proxy'
import bodyParser from 'body-parser' import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser' import cookieParser from 'cookie-parser'
import session from 'express-session' import session from 'express-session'
@ -15,66 +16,66 @@ import connectRedis from 'connect-redis'
import passport from 'passport' import passport from 'passport'
import mongoose from 'mongoose' import mongoose from 'mongoose'
import { authRouter, initPassport } from './auth/passport'; import { authRouter, initPassport } from './auth/passport'
import ensureAuthenticated from './auth/middleware';
const RedisStore = connectRedis(session); const RedisStore = connectRedis(session)
const redisClient = redis.createClient({ const redisClient = redis.createClient({
host: '127.0.0.1', host: '127.0.0.1',
port: 6379, port: 6379,
password: process.env.REDIS_PASSWORD password: process.env.REDIS_PASSWORD,
}) })
redisClient.on('error', (error) => { redisClient.on('error', (error) => {
console.error(error); console.error(error)
}); })
redisClient.on('connect', () => { redisClient.on('connect', () => {
console.log('Connection to Redis has been established successfully.'); console.log('Connection to Redis has been established successfully.')
});
mongoose.connect('mongodb://localhost:27017', {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
user: process.env.MONGODB_USER,
pass: process.env.MONGODB_PASSWORD,
dbName: 'museum'
}, () => {
console.log('Connection to MongoDB has been established successfully.')
}) })
const app = express(); mongoose.connect(
app.set('trust proxy', 1); 'mongodb://localhost:27017',
const server = http.createServer(app); {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
user: process.env.MONGODB_USER,
pass: process.env.MONGODB_PASSWORD,
dbName: 'museum',
},
() => {
console.log('Connection to MongoDB has been established successfully.')
},
)
const app = express()
app.set('trust proxy', 1)
const server = http.createServer(app)
app.use(bodyParser.json()); app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser(process.env.SESSION_SECRET)); app.use(cookieParser(process.env.SESSION_SECRET))
app.use( app.use(
session({ session({
store: new RedisStore({ client: redisClient }), store: new RedisStore({ client: redisClient }),
secret: process.env.SESSION_SECRET, secret: process.env.SESSION_SECRET,
resave: false, resave: false,
saveUninitialized: false, saveUninitialized: false,
}) }),
); )
app.use(passport.initialize()); app.use(passport.initialize())
app.use(passport.session()); app.use(passport.session())
initPassport(); initPassport()
app.use('/auth', authRouter); app.use('/auth', authRouter)
app.use(express.static(path.resolve(__dirname, '../static'))); if (process.env.NODE_ENV !== 'PRODUCTION') {
const proxy = require('express-http-proxy')
app.get('/', (_req, res) => { app.use('/', proxy('http://localhost:4000/'))
res.render('index'); } else {
}); // probably serve up build version in production
}
app.get('/museo', ensureAuthenticated, (req, res) => {
res.render('museo', { user: req.user })
});
server.listen(3000, () => { server.listen(3000, () => {
console.log('> Ready on http://localhost:3000'); console.log('> Ready on http://localhost:3000')
}); })

Loading…
Cancel
Save