This commit is contained in:
Alexandre Bodin 2022-09-09 17:46:43 +02:00 committed by Alexandre BODIN
parent 410c0cef76
commit de2ed2bfa4
6 changed files with 35 additions and 1 deletions

View File

@ -0,0 +1,9 @@
const { requestContext } = require('@strapi/strapi');
module.exports = {
beforeCreate() {
const store = requestContext.getStore();
console.log('User info in service: ', store.auth);
},
};

View File

@ -5,7 +5,7 @@ const { createCoreRouter } = require('@strapi/strapi').factories;
module.exports = createCoreRouter('api::address.address', {
config: {
find: {
auth: false,
// auth: false,
},
},
only: ['find', 'findOne'],

View File

@ -3,6 +3,7 @@
const Strapi = require('./Strapi');
Strapi.factories = require('./factories');
Strapi.requestContext = require('./services/request-context');
Strapi.compile = require('./compile');
module.exports = Strapi;

View File

@ -4,6 +4,7 @@ const { strict: assert } = require('assert');
const { has, prop } = require('lodash/fp');
const { UnauthorizedError } = require('@strapi/utils').errors;
const requestCtx = require('../request-context');
const INVALID_STRATEGY_MSG =
'Invalid auth strategy. Expecting an object with properties {name: string, authenticate: function, verify: function}';
@ -60,6 +61,9 @@ const createAuthentication = () => {
credentials,
};
const store = requestCtx.getStore();
store.auth = ctx.state.auth;
return next();
}
}

View File

@ -0,0 +1,5 @@
'use strict';
const { AsyncLocalStorage } = require('async_hooks');
module.exports = new AsyncLocalStorage();

View File

@ -9,6 +9,7 @@ const { createContentAPI } = require('./content-api');
const registerAllRoutes = require('./register-routes');
const registerApplicationMiddlewares = require('./register-middlewares');
const createKoaApp = require('./koa');
const requestCtx = require('../request-context');
const healthCheck = async (ctx) => {
ctx.set('strapi', 'You are so French!');
@ -33,6 +34,20 @@ const createServer = (strapi) => {
keys: strapi.config.get('server.app.keys'),
});
app.use(async (ctx, next) => {
const store = {
foo: 'bar',
};
// TODO: handle errors
await new Promise((res) => {
requestCtx.run(store, async () => {
await next();
res();
});
});
});
const router = new Router();
const routeManager = createRouteManager(strapi);