Skip to content

Commit

Permalink
Add SDKConfig and SDKContext to core
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLo123 committed Apr 8, 2024
1 parent 4d52fa8 commit a26a79d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
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 type 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 './SDKConfig';
export * from './SDKContext';
export * from './UrlHelpers';
export * from './TokenRefresher';

0 comments on commit a26a79d

Please sign in to comment.