Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise(resolve => {
const session = getNamespace('webiny-api');
const req = session.get('req');
const token = req.get('Webiny-Authorization');
if (!token) {
resolve(null);
}
jwt.verify(token, this.config.jwtSecret, (err, decoded) => {
if (err) {
// TODO: check if token is expired
resolve(null);
} else {
this.config.entity.findById(decoded.id).then(user => {
this.user = user;
resolve(user);
}).catch(e => {
export function getNamespace() {
return getClsNamespace(APP_CONTINUATION_KEY);
}
public get store() {
return getNamespace(ContextTypes.ROOT);
}
}
static currentRequestContext(): RequestContext {
const session = cls.getNamespace(RequestContext.name);
if (session && session.active) {
return session.get(RequestContext.name);
}
return null;
}
export const getTransactionalContextHook = () => {
const ctx = getNamespace(NAMESPACE_NAME)
const emitter = getHookInContext(ctx)
if (!emitter) {
throw new Error('No hook manager found in context. Are you using @Transactional()?')
}
return emitter
}
descriptor.value = function(...args: any[]) {
const context = getNamespace(NAMESPACE_NAME)
if (!context) {
throw new Error(
'No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.'
)
}
const runOriginal = async () => originalMethod.apply(this, [...args])
const runWithNewHook = async () => runInNewHookContext(context, runOriginal)
const runWithNewTransaction = async () => {
const transactionCallback = async (entityManager: EntityManager) => {
setEntityManagerForConnection(connectionName, context, entityManager)
const result = await originalMethod.apply(this, [...args])
setEntityManagerForConnection(connectionName, context, null)
return result
}
use(req, res, next) {
const requestContext = new RequestContext(req, res);
const session = cls.getNamespace(RequestContext.name) || cls.createNamespace(RequestContext.name);
session.run(async () => {
session.set(RequestContext.name, requestContext);
next();
});
}
}
export const getEntityManagerOrTransactionManager = (
connectionName: string,
entityManager: EntityManager | undefined
): EntityManager => {
const context = getNamespace(NAMESPACE_NAME)
if (context && context.active) {
const transactionalEntityManager = getEntityManagerForConnection(connectionName, context)
if (transactionalEntityManager) {
return transactionalEntityManager
}
}
return entityManager || getManager(connectionName)
}
export const initializeTransactionalContext = () =>
getNamespace(NAMESPACE_NAME) || createNamespace(NAMESPACE_NAME)