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

Add SDKConfig and SDKContext to core #58

Merged
merged 1 commit into from
Apr 8, 2024
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
69 changes: 69 additions & 0 deletions packages/core/src/SDKConfig/SDKConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Config for FusionAuth Web SDKs
*/
export type SDKConfig = {
/**
* The URL of the FusionAuth server.
*/
serverUrl: string;

/**
* The client id of the application.
*/
clientId: string;

/**
* The redirect URI of the application.
*/
redirectUri: string;

/**
* The redirect URI for post-logout. Defaults the provided `redirectUri`.
*/
postLogoutRedirectUri?: string;

/**
* Enables automatic token refreshing. Defaults to false.
*/
shouldAutoRefresh?: boolean;

/**
* Enables the SDK to automatically handle fetching user info when logged in. Defaults to false.
*/
shouldAutoFetchUserInfo?: boolean;

/**
* The number of seconds before the access token expiry when the auto refresh functionality kicks in if enabled. Default is 30.
*/
autoRefreshSecondsBeforeExpiry?: number;

/**
* Callback function to be invoked with the `state` value upon redirect from login or register.
*/
onRedirect?: (state?: string) => void;

/**
* The path to the login endpoint.
*/
loginPath?: string;

/**
* The path to the register endpoint.
*/
registerPath?: string;

/**
* The path to the logout endpoint.
*/
logoutPath?: string;

/**
* The path to the token refresh endpoint.
*/
tokenRefreshPath?: string;

/**
* The path to the me endpoint.
*/
mePath?: string;
};
1 change: 1 addition & 0 deletions packages/core/src/SDKConfig/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SDKConfig';
75 changes: 75 additions & 0 deletions packages/core/src/SDKContext/SDKContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/** The context provided by FusionAuth Web SDKs */
export interface SDKContext {
/**
* Whether the user is logged in.
*/
isLoggedIn: boolean;

/**
* Data fetched from the 'me' endpoint.
*/
userInfo?: UserInfo;

/**
* Fetches user info from the 'me' endpoint.
* This is handled automatically if the SDK is configured with `shouldAutoFetchUserInfo`.
* @returns {Promise<UserInfo>}
*/
fetchUserInfo: () => Promise<UserInfo>;

/**
* Indicates that the fetchUserInfo call is unresolved.
*/
isFetchingUserInfo: boolean;

/**
* Error occurred while fetching userInfo.
*/
error?: Error;

/**
* Initiates login flow.
* @param {string} [state] - Optional value to be echoed back to the SDK upon redirect.
*/
startLogin: (state?: string) => void;

/**
* Initiates register flow.
* @param {string} [state] - Optional value to be echoed back to the SDK upon redirect.
*/
startRegister: (state?: string) => void;

/**
* Initiates logout flow.
*/
startLogout: () => void;

/**
* Refreshes the access token a single time.
* This is handled automatically if the SDK is configured with `shouldAutoRefresh`.
*/
refreshToken: () => Promise<void>;

/**
* Initializes automatic access token refreshing.
* This is handled automatically if the SDK is configured with `shouldAutoRefresh`.
*/
initAutoRefresh: () => void;
}

/**
* User information returned from FusionAuth.
*/
export type UserInfo = {
applicationId?: string;
email?: string;
email_verified?: boolean;
family_name?: string;
given_name?: string;
picture?: string;
roles?: any[];
sid?: string;
sub?: string;
tid?: string;
phone_number?: string;
};
1 change: 1 addition & 0 deletions packages/core/src/SDKContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SDKContext';
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './CookieHelpers';
export * from './UrlHelper';
export * from './SDKConfig';
export * from './SDKContext';
export * from './TokenRefresher';