diff --git a/lib/compute-schema.js b/lib/compute-schema.js index 00b6d332..e065281c 100644 --- a/lib/compute-schema.js +++ b/lib/compute-schema.js @@ -2,9 +2,7 @@ const { asyncLocalStorage, defaultStore } = require('./async-storage') const computeSchema = (req, res, next) => { const store = defaultStore() - asyncLocalStorage.run(store, () => { - next() - }) + return asyncLocalStorage.run(store, () => next()) } module.exports = computeSchema diff --git a/lib/db.js b/lib/db.js index 3e58ac2b..43c25537 100644 --- a/lib/db.js +++ b/lib/db.js @@ -38,7 +38,10 @@ const _task = (obj, opts, cb) => { }) } -const getSchema = () => 'public' +const getSchema = () => { + const store = asyncLocalStorage.getStore() ?? defaultStore() + return asyncLocalStorage.run(store, () => store.get('schema')) +} const getDefaultSchema = () => 'ERROR_SCHEMA' const searchPathWrapper = (t, cb) => { diff --git a/lib/new-admin/admin-server.js b/lib/new-admin/admin-server.js index 8b7938cf..b1ccda5d 100644 --- a/lib/new-admin/admin-server.js +++ b/lib/new-admin/admin-server.js @@ -44,8 +44,9 @@ app.use(express.json()) app.use(express.urlencoded({ extended: true })) // support encoded bodies app.use(express.static(path.resolve(__dirname, '..', '..', 'public'))) app.use(cleanUserSessions(USER_SESSIONS_CLEAR_INTERVAL)) -app.use(session) app.use(computeSchema) +app.use(findOperatorId) +app.use(session) const apolloServer = new ApolloServer({ typeDefs, diff --git a/lib/new-admin/middlewares/context.js b/lib/new-admin/middlewares/context.js index db4687d2..aeba75eb 100644 --- a/lib/new-admin/middlewares/context.js +++ b/lib/new-admin/middlewares/context.js @@ -1,7 +1,7 @@ const users = require('../../users') const buildApolloContext = async ({ req, res }) => { - if (!req.session.user) return { req } + if (!req.session.user) return { req, res } const user = await users.verifyAndUpdateUser( req.session.user.id, @@ -14,12 +14,15 @@ const buildApolloContext = async ({ req, res }) => { req.session.ipAddress = req.ip req.session.lastUsed = new Date(Date.now()).toISOString() req.session.user.id = user.id + req.session.user.username = user.username req.session.user.role = user.role + res.set('role', user.role) + res.cookie('email', user.username) res.set('Access-Control-Expose-Headers', 'role') - return { req } + return { req, res } } module.exports = buildApolloContext diff --git a/lib/routes.js b/lib/routes.js index 4c694fd5..0ed9c557 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -31,7 +31,6 @@ const verifyTxRoutes = require('./routes/verifyTxRoutes') const verifyPromoCodeRoutes = require('./routes/verifyPromoCodeRoutes') const app = express() -const localApp = express() const configRequiredRoutes = [ '/poll', @@ -85,4 +84,4 @@ app.use((req, res) => { res.status(404).json({ error: 'No such route' }) }) -module.exports = { app, localApp } +module.exports = { app } diff --git a/new-lamassu-admin/src/pages/Authentication/Input2FAState.js b/new-lamassu-admin/src/pages/Authentication/Input2FAState.js index 2cb6cce1..dad864f3 100644 --- a/new-lamassu-admin/src/pages/Authentication/Input2FAState.js +++ b/new-lamassu-admin/src/pages/Authentication/Input2FAState.js @@ -56,7 +56,17 @@ const Input2FAState = ({ state, dispatch }) => { const [input2FA, { error: mutationError }] = useMutation(INPUT_2FA, { onCompleted: ({ input2FA: success }) => { - success ? getUserData() : setInvalidToken(true) + if (success) { + return getUserData({ + context: { + headers: { + email: state.clientField, + 'Access-Control-Expose-Headers': 'email' + } + } + }) + } + return setInvalidToken(true) } }) @@ -82,6 +92,11 @@ const Input2FAState = ({ state, dispatch }) => { password: state.passwordField, code: state.twoFAField, rememberMe: state.rememberMeField + }, + context: { + headers: { + email: state.clientField + } } }) } diff --git a/new-lamassu-admin/src/pages/Authentication/LoginState.js b/new-lamassu-admin/src/pages/Authentication/LoginState.js index ef6f8d1b..936c7cd5 100644 --- a/new-lamassu-admin/src/pages/Authentication/LoginState.js +++ b/new-lamassu-admin/src/pages/Authentication/LoginState.js @@ -53,6 +53,11 @@ const LoginState = ({ state, dispatch }) => { variables: { username, password + }, + context: { + headers: { + email: username + } } }) diff --git a/new-lamassu-admin/src/pages/Authentication/Setup2FAState.js b/new-lamassu-admin/src/pages/Authentication/Setup2FAState.js index 346291b8..abf2c2e5 100644 --- a/new-lamassu-admin/src/pages/Authentication/Setup2FAState.js +++ b/new-lamassu-admin/src/pages/Authentication/Setup2FAState.js @@ -69,6 +69,7 @@ const Setup2FAState = ({ state, dispatch }) => { const { error: queryError } = useQuery(GET_2FA_SECRET, { variables: { username: state.clientField, password: state.passwordField }, + context: { headers: { email: state.clientField } }, onCompleted: ({ get2FASecret }) => { setSecret(get2FASecret.secret) setOtpauth(get2FASecret.otpauth) @@ -84,7 +85,16 @@ const Setup2FAState = ({ state, dispatch }) => { const [setup2FA, { error: mutationError }] = useMutation(SETUP_2FA, { onCompleted: ({ setup2FA: success }) => { - success ? getUserData() : setInvalidToken(true) + success + ? getUserData({ + context: { + headers: { + email: state.clientField, + 'Access-Control-Expose-Headers': 'email' + } + } + }) + : setInvalidToken(true) } }) @@ -155,7 +165,8 @@ const Setup2FAState = ({ state, dispatch }) => { password: state.passwordField, rememberMe: state.rememberMeField, codeConfirmation: twoFAConfirmation - } + }, + context: { headers: { email: state.clientField } } }) }} buttonClassName={classes.loginButton}> diff --git a/new-lamassu-admin/src/routing/PrivateRoute.js b/new-lamassu-admin/src/routing/PrivateRoute.js index 2861612d..c17ba94f 100644 --- a/new-lamassu-admin/src/routing/PrivateRoute.js +++ b/new-lamassu-admin/src/routing/PrivateRoute.js @@ -8,6 +8,8 @@ import { isLoggedIn } from './utils' const PrivateRoute = ({ ...rest }) => { const { userData } = useContext(AppContext) + console.log('isLoggedIn', isLoggedIn(userData)) + return isLoggedIn(userData) ? : } diff --git a/new-lamassu-admin/src/utils/apollo.js b/new-lamassu-admin/src/utils/apollo.js index 5512815d..594490fb 100644 --- a/new-lamassu-admin/src/utils/apollo.js +++ b/new-lamassu-admin/src/utils/apollo.js @@ -12,7 +12,7 @@ import AppContext from 'src/AppContext' const URI = process.env.NODE_ENV === 'development' ? 'https://localhost:8070' : '' -const getClient = (history, location, setUserData, setRole) => +const getClient = (history, location, getUserData, setUserData, setRole) => new ApolloClient({ link: ApolloLink.from([ onError(({ graphQLErrors, networkError }) => { @@ -67,8 +67,14 @@ const getClient = (history, location, setUserData, setRole) => const Provider = ({ children }) => { const history = useHistory() const location = useLocation() - const { setUserData, setRole } = useContext(AppContext) - const client = getClient(history, location, setUserData, setRole) + const { userData, setUserData, setRole } = useContext(AppContext) + const client = getClient( + history, + location, + () => userData, + setUserData, + setRole + ) return {children} }