@adonis-agora/media
Version:
Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a
71 lines • 2.74 kB
JavaScript
const importServer = () => import('@adonis-agora/authkit-server');
const importClient = () => import('@adonis-agora/authkit-client');
/**
* `access.identity` for apps on authkit — who the current user is, as authkit sees them:
*
* ```ts
* // config/media.ts
* import { authkitIdentity } from '@adonis-agora/media/authkit'
* export default defineConfig({ access: { identity: authkitIdentity() } })
* ```
*
* On the server side, `id` is the account the request acts as (an impersonating admin sees the
* user's files) and `realId` the human behind it (what `@adonis-agora/media/authz` checks
* permissions for). On the client side both are the token's `sub`, with its global roles and
* active organization as `roles` / `tenantId`.
*/
export function authkitIdentity(options = {}) {
const side = options.side ?? 'auto';
const type = options.type;
const loadServer = options.load?.server ?? importServer;
const loadClient = options.load?.client ?? importClient;
const fromServer = async (ctx) => {
const server = await loadServer();
const id = server.getAccountId(ctx);
if (!id)
return null;
return { id, realId: server.realAccountId(ctx) ?? id, ...(type ? { type } : {}) };
};
const fromClient = async (ctx) => {
const client = await loadClient();
const identity = await (await client.getAuthkit(ctx)).getIdentity();
if (!identity)
return null;
return {
id: identity.userId,
realId: identity.userId,
roles: identity.globalRoles ?? [],
tenantId: identity.orgId ?? null,
...(type ? { type } : {}),
};
};
return async (ctx) => {
if (side === 'server')
return fromServer(ctx);
if (side === 'client')
return fromClient(ctx);
if (ctx.authkit)
return fromClient(ctx);
try {
return await fromServer(ctx);
}
catch (error) {
if (!isModuleNotFound(error))
throw error;
}
try {
return await fromClient(ctx);
}
catch (error) {
if (!isModuleNotFound(error))
throw error;
throw new Error("authkitIdentity() needs '@adonis-agora/authkit-server' (an identity-provider app) or " +
"'@adonis-agora/authkit-client' (an app signing in against one), and neither is " +
'installed. Install the one this app uses, or set `access.identity` to another resolver.', { cause: error });
}
};
}
function isModuleNotFound(error) {
return error?.code === 'ERR_MODULE_NOT_FOUND';
}
//# sourceMappingURL=authkit.js.map