mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import type { Context } from 'koa';
|
|
import type { InteractionResults } from 'oidc-provider';
|
|
import type Provider from 'oidc-provider';
|
|
|
|
import type Queries from '#src/tenants/Queries.js';
|
|
|
|
export const assignInteractionResults = async (
|
|
ctx: Context,
|
|
provider: Provider,
|
|
result: InteractionResults,
|
|
merge = false
|
|
) => {
|
|
// The "mergeWithLastSubmission" will only merge current request's interaction results,
|
|
// which is stored in ctx.oidc, we need to merge interaction results in two requests,
|
|
// have to do it manually
|
|
// refer to: https://github.com/panva/node-oidc-provider/blob/c243bf6b6663c41ff3e75c09b95fb978eba87381/lib/actions/authorization/interactions.js#L106
|
|
const details = merge ? await provider.interactionDetails(ctx.req, ctx.res) : undefined;
|
|
|
|
const redirectTo = await provider.interactionResult(
|
|
ctx.req,
|
|
ctx.res,
|
|
{
|
|
// Merge with current result
|
|
...details?.result,
|
|
...result,
|
|
},
|
|
{
|
|
mergeWithLastSubmission: merge,
|
|
}
|
|
);
|
|
ctx.body = { redirectTo };
|
|
};
|
|
|
|
export const saveUserFirstConsentedAppId = async (
|
|
queries: Queries,
|
|
userId: string,
|
|
applicationId: string
|
|
) => {
|
|
const { findUserById, updateUserById } = queries.users;
|
|
const { applicationId: firstConsentedAppId } = await findUserById(userId);
|
|
|
|
if (!firstConsentedAppId) {
|
|
// Save application id that the user first consented
|
|
await updateUserById(userId, { applicationId });
|
|
}
|
|
};
|