diff --git a/packages/core/src/SDKConfig/SDKConfig.ts b/packages/core/src/SDKConfig/SDKConfig.ts new file mode 100644 index 0000000..cd4eb5f --- /dev/null +++ b/packages/core/src/SDKConfig/SDKConfig.ts @@ -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; +}; diff --git a/packages/core/src/SDKConfig/index.ts b/packages/core/src/SDKConfig/index.ts new file mode 100644 index 0000000..3ddb375 --- /dev/null +++ b/packages/core/src/SDKConfig/index.ts @@ -0,0 +1 @@ +export * from './SDKConfig'; diff --git a/packages/core/src/SDKContext/SDKContext.ts b/packages/core/src/SDKContext/SDKContext.ts new file mode 100644 index 0000000..f1f562f --- /dev/null +++ b/packages/core/src/SDKContext/SDKContext.ts @@ -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} + */ + fetchUserInfo: () => Promise; + + /** + * 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; + + /** + * 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; +}; diff --git a/packages/core/src/SDKContext/index.ts b/packages/core/src/SDKContext/index.ts new file mode 100644 index 0000000..eb8187b --- /dev/null +++ b/packages/core/src/SDKContext/index.ts @@ -0,0 +1 @@ +export * from './SDKContext'; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 758cf8f..359030c 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,3 +1,5 @@ export * from './CookieHelpers'; +export * from './SDKConfig'; +export * from './SDKContext'; export * from './UrlHelpers'; export * from './TokenRefresher';