Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for onExistingPasswordProvided workflow event #35

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ 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>;
// eslint-disable-next-line @typescript-eslint/no-explicit-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 +240,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 +277,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.secureFetch(url, options);

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

/**
* create a Kinde API client
* @param baseURL Base URL of the Kinde API
Expand Down
28 changes: 26 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ export type WorkflowSettings = {
*/
console?: {};
/**
* Exposes the fetch method to call extenal APIs to the workflow
* Exposes the fetch method to call external APIs to the workflow
*/
"kinde.fetch"?: {};
/**
* Exposes the fetch method to call signed external 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;
DanielRivers marked this conversation as resolved.
Show resolved Hide resolved

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

export type onExistingPasswordProvided = EventBase & {
context: {
DanielRivers marked this conversation as resolved.
Show resolved Hide resolved
auth: {
password: string;
};
user: {
id: string;
};
workflow: {
DanielRivers marked this conversation as resolved.
Show resolved Hide resolved
trigger: WorkflowTrigger.ExistingPasswordProvided;
};
};
};

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