Skip to content

Commit

Permalink
feat: add support for onExistingPasswordProvided workflow event
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRivers committed Jan 15, 2025
1 parent b97220f commit 02f2be6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
48 changes: 47 additions & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ const getAssetUrl = (assetPath: string, orgCode?: OrgCode) => {
declare namespace kinde {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function fetch(url: string, options: unknown): Promise<any>;
export function secureFetch(url: string, options: unknown): Promise<any>;

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace env {
export function get(key: string): { value: string; isSecret: boolean };
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace widget {
export function invalidateFormField(fieldName: string, message: string): void;
}

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace localization {
export function get(key: string): string;
Expand Down Expand Up @@ -230,9 +236,22 @@ export function denyAccess(reason: string) {
kinde.auth.denyAccess(reason);
}

/**
* Invalidate a Kinde widget form field
* @param fieldName Name of the field to invalidate
* @param message Reason for invalidating the field
*/
export function invalidateFormField(fieldName: string, message: string) {
if (!kinde.widget) {
throw new Error("widget binding not available");
}
kinde.widget.invalidateFormField(fieldName, message);
}

/**
* Fetch data from an external API
* @param reason Reason for denying access
* @param url URL of the API
* @param options Fetch options
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function fetch<T = any>(
Expand All @@ -254,6 +273,33 @@ export async function fetch<T = any>(
} as T;
}

/**
* Fetch data from a secure external API
*
* Encryption keys can be setup in the Kinde dashboard under workflows > encryption keys
* @param url URL of the API
* @param options Fetch options
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function secureFetch<T = any>(
url: string,
options: KindeFetchOptions,
): Promise<T> {
if (!kinde.secureFetch) {
throw new Error("secureFetch binding not available");
}

if (!options.responseFormat) {
options.responseFormat = "json";
}

const result = await kinde.fetch(url, options);

return {
data: result?.json,
} as T;
}

/**
* create a Kinde API client
* @param baseURL Base URL of the Kinde API
Expand Down
26 changes: 25 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export type WorkflowSettings = {
* Exposes the fetch method to call extenal APIs to the workflow
*/
"kinde.fetch"?: {};
/**
* Exposes the fetch method to call signed extenal APIs to the workflow
*/
"kinde.secureFetch"?: {},
/**
* Exposes the fetch method to call access the manipulate the Kinde widget
*/
"kinde.widget"?: {},
/**
* Exposes access to the kinde environment variables
*/
Expand All @@ -77,11 +85,13 @@ export type WorkflowSettings = {
export enum WorkflowTrigger {
UserTokenGeneration = "user:tokens_generation",
M2MTokenGeneration = "m2m:token_generation",
ExistingPasswordProvided = "user:existing_password_provided",
}

export type WorkflowEvents =
| onUserTokenGeneratedEvent
| onM2MTokenGeneratedEvent;
| onM2MTokenGeneratedEvent
| onExistingPasswordProvided;

type EventBase = {
request: RequestContext;
Expand Down Expand Up @@ -122,6 +132,20 @@ export type onUserTokenGeneratedEvent = EventBase & {
};
};

export type onExistingPasswordProvided = EventBase & {
context: {
auth: {
password: string
};
user: {
id: string;
};
workflow: {
trigger: WorkflowTrigger.ExistingPasswordProvided;
};
};
};

export type onM2MTokenGeneratedEvent = EventBase & {
context: {
workflow: {
Expand Down

0 comments on commit 02f2be6

Please sign in to comment.