diff --git a/packages/core/src/api/branding-preference-text.ts b/packages/core/src/api/branding-preference-text.ts index 127d0822..38a1a7d3 100644 --- a/packages/core/src/api/branding-preference-text.ts +++ b/packages/core/src/api/branding-preference-text.ts @@ -30,7 +30,7 @@ import {BrandingPreferenceTextAPIResponse} from 'src/models/branding-text-api-re * @returns A Promise that resolves to the response from the server. * @throws {AsgardeoUIException} If the API call fails or when the response is not successful. */ -export const getBrandingPreferenceText = async ( +const getBrandingPreferenceText = async ( locale: string, name: string, screen: string, @@ -71,3 +71,5 @@ export const getBrandingPreferenceText = async ( 'Failed to receive a successful response from the branding text endpoint', ); }; + +export default getBrandingPreferenceText; diff --git a/packages/core/src/api/profile.ts b/packages/core/src/api/profile.ts new file mode 100644 index 00000000..c515f96c --- /dev/null +++ b/packages/core/src/api/profile.ts @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import {AuthClient} from 'src/auth-client'; +import AsgardeoUIException from 'src/exception'; +import {MeAPIResponse} from 'src/models/me-api-response'; + +/** + * Fetch the profile information of the authenticated user. + * + * This function uses the `AuthClient` instance to get the base URL and access token, + * and then makes a GET request to the `/scim2/Me` endpoint to fetch the user's profile information. + * + * @returns {Promise} A promise that resolves to an object containing the user's profile information. + * @throws {AsgardeoUIException} Throws an exception if there's an error getting the base URL and access token, or if the fetch request fails. + */ +const getProfileInformation = async (): Promise => { + let baseUrl: string; + let accessToken: string; + let response: Response; + + try { + baseUrl = (await AuthClient.getInstance().getDataLayer().getConfigData()).baseUrl; + accessToken = await AuthClient.getInstance().getAccessToken(); + } catch (error) { + throw new AsgardeoUIException( + 'JS_UI_CORE-ME-GPI-NF', + 'Failed in getting the base URL and access token.', + error.stack, + ); + } + + if (!accessToken) { + throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-IV', 'Access token is null.'); + } + + const headers: Headers = new Headers(); + headers.append('Authorization', `Bearer ${accessToken}`); + headers.append('Content-Type', 'application/json'); + + const requestOptions: RequestInit = { + headers, + method: 'GET', + }; + + try { + response = await fetch(new Request(`${baseUrl}/scim2/Me`, requestOptions)); + } catch (error) { + throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-NE', 'Me API call failed.', error.stack); + } + + if (response.ok) { + return (await response.json()) as MeAPIResponse; + } + + throw new AsgardeoUIException('JS_UI_CORE-ME-GPI-HE', 'Failed to receive a successful response from the Me API.'); +}; + +export default getProfileInformation; diff --git a/packages/core/src/api/public-api.ts b/packages/core/src/api/public-api.ts new file mode 100644 index 00000000..f87cf4a4 --- /dev/null +++ b/packages/core/src/api/public-api.ts @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export {default as authorize} from './authorize'; +export {default as authenticate} from './authenticate'; +export {default as getBrandingPreference} from './branding-preference'; +export {default as getProfileInformation} from './profile'; +export {default as getBrandingPreferenceText} from './branding-preference-text'; diff --git a/packages/core/src/branding/branding.ts b/packages/core/src/branding/branding.ts index 688db1f7..bae28eed 100644 --- a/packages/core/src/branding/branding.ts +++ b/packages/core/src/branding/branding.ts @@ -55,3 +55,5 @@ export const getBranding = async (props: GetBranding): Promise => return mergedBranding; }; + +export default getBranding; diff --git a/packages/core/src/i18n/i18n.ts b/packages/core/src/i18n/i18n.ts index e9994c09..6347f17b 100644 --- a/packages/core/src/i18n/i18n.ts +++ b/packages/core/src/i18n/i18n.ts @@ -17,7 +17,7 @@ */ import merge from 'lodash.merge'; -import {getBrandingPreferenceText} from 'src/api/branding-preference-text'; +import getBrandingPreferenceText from 'src/api/branding-preference-text'; import {AuthClient} from 'src/auth-client'; import {BrandingPreferenceTextAPIResponse} from 'src/models/branding-text-api-response'; import {GetLocalization, TextObject} from './screens/model'; diff --git a/packages/core/src/i18n/public.ts b/packages/core/src/i18n/public.ts index 09e800ac..34ffb7ba 100644 --- a/packages/core/src/i18n/public.ts +++ b/packages/core/src/i18n/public.ts @@ -18,3 +18,4 @@ export * from './screens/model'; export * from './screens/keys'; +export {default as getLocalization} from './i18n'; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index e69de29b..588ab8b7 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export * from './api/public-api'; +export {default as getBranding} from './branding/branding'; +export * from './i18n/public'; +export * from './auth-client'; +export * from './models/public-models'; diff --git a/packages/core/src/models/me-api-response.ts b/packages/core/src/models/me-api-response.ts new file mode 100644 index 00000000..8b9fe3e7 --- /dev/null +++ b/packages/core/src/models/me-api-response.ts @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * The interface for the response from the "me" API endpoint. + */ +export interface MeAPIResponse { + /** + * The user's email addresses. + */ + emails: string[]; + /** + * The user's id. + */ + id: string; + /** + * The user's names. + */ + name: Name; + /** + * When signed in using a social login, the photos field will be populated instead of profile URL. + */ + photos?: Photos[]; + /** + * The user's profile URL. + */ + profileUrl: string; + /** + * The user's username. + */ + userName: string; +} + +/** + * The interface for the name field in the "me" API response. + */ +export interface Name { + familyName?: string; + givenName?: string; +} + +/** + * The interface for the photos field in the "me" API response. + */ +export interface Photos { + type: string; + value: string; +} diff --git a/packages/core/src/models/public-models.ts b/packages/core/src/models/public-models.ts new file mode 100644 index 00000000..f770cf47 --- /dev/null +++ b/packages/core/src/models/public-models.ts @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export * from './auth-api-request'; +export * from './auth-api-response'; +export * from './auth-config'; +export * from './branding-api-response'; +export * from './customization'; +export * from './me-api-response'; +export * from './screen-type';